Android 實現系統分享


 

使用手機上的程序,來分享/發送,比如QQ的“發送到我的電腦”。

 

1、分享/發送文本內容

1  Intent shareIntent = new Intent();
2         shareIntent.setAction(Intent.ACTION_SEND);
3         shareIntent.setType("text/plain");
4         //要分享的文本內容,選擇某項后會直接把這段文本發送出去,相當於調用選中的應用的接口,並傳參
5         shareIntent.putExtra(Intent.EXTRA_TEXT, "Here is the Shared text.");
6         //需要使用Intent.createChooser,這里我們直接復用。第二個參數並不會顯示出來
7         shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
8         startActivity(shareIntent);

 

 

通用步驟:

首先將Intent的cation設置為Intent.ACTION_SEND,

其次根據分享的內容設置不同的Type,

然后根據不同的社交平台設置相關Extras,

最后創建並啟動選擇器

 

 

2、分享/發送單張圖片

1  //指定要分享的圖片路徑
2         Uri imgUri = Uri.parse("mnt/sdcard/1.jpg");
3         Intent shareIntent = new Intent();
4         shareIntent.setAction(Intent.ACTION_SEND);
5         shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
6         shareIntent.setType("image/*");
7         shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
8         startActivity(shareIntent);

注意根目錄為     mnt/sdcard/   

 

 

3、分享/發送多張圖片

 1  Uri imgUri1 = Uri.parse("mnt/sdcard/1.jpg");
 2         Uri imgUri2 = Uri.parse("mnt/sdcard/2.jpg");
 3         ArrayList<Uri> imgUris = new ArrayList<Uri>();   //使用集合保存
 4         imgUris.add(imgUri1); 
 5         imgUris.add(imgUri2); 
 6 
 7         Intent shareIntent = new Intent();
 8         shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);    //注意是Intent_ACTION_MULTIPLE
 9         shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imgUris);   //注意是方法是putParcelableArrayListExtra()
10         shareIntent.setType("image/*");
11         shareIntent = Intent.createChooser(shareIntent, "Here is the title of Select box");
12         startActivity(shareIntent);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM