android 使用Intent.ACTION_SEND分享图片和文字内容(新浪微博,短信等)

下面的方法只能实现普通的文字分享:

1
2
3
4
5
6
7
8
9
10
11
private void shareContent() {
         Intent share = new Intent(android.content.Intent.ACTION_SEND);
         share.setType( "text/plain" );
         String title = "标题" ;
         String extraText= "给大家介绍一个好网站,www.jcodecraeer.com" ;
         share.putExtra(Intent.EXTRA_TEXT, extraText);
         if (title != null ) {
             share.putExtra(Intent.EXTRA_SUBJECT, title);
         }
         startActivity(Intent.createChooser(share, "分享一下" ));
    }

那如果我想同时分享图片和文字到新浪微博的话,则使用下面的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void share(String content, Uri uri){
     Intent shareIntent = new Intent(Intent.ACTION_SEND);
     if (uri!= null ){
         shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
         shareIntent.setType( "image/*" );
         //当用户选择短信时使用sms_body取得文字
         shareIntent.putExtra( "sms_body" , content);
     } else {
         shareIntent.setType( "text/plain" );
     }
     shareIntent.putExtra(Intent.EXTRA_TEXT, content);
     //自定义选择框的标题
     startActivity(Intent.createChooser(shareIntent, "邀请好友" ));
     //系统默认标题
                                                                                                                
}

之所以这种方法可以传递图片,是因为shareIntent.setType("image/*"),而 setType("image/*")可以传递文字也可以传递图片;其中图片内容可以由Uri指定,注意需要将图片的url转换成uri。

你可能感兴趣的:(安卓-csdn)