Android自帶API實現(xiàn)分享功能
更新時間:2018年04月25日 16:31:16 作者:夏沫凡塵
這篇文章主要為大家詳細介紹了Android自帶API實現(xiàn)分享功能,實現(xiàn)文字和圖片的分享,具有一定的參考價值,感興趣的小伙伴們可以參考一下
前言
在做項目的過程中需要實現(xiàn)文字和圖片的分享,有兩種方式:
1. 使用android sdk中自帶的Intent.ACTION_SEND實現(xiàn)分享。
2. 使用shareSDK、友盟等第三方的服務(wù)。
鑒于使用的方便,此次只介紹使用Android sdk中自帶的方式來實現(xiàn)分享的功能。
分享文字
/** * 分享文字內(nèi)容 * * @param dlgTitle * 分享對話框標(biāo)題 * @param subject * 主題 * @param content * 分享內(nèi)容(文字) */ private void shareText(String dlgTitle, String subject, String content) { if (content == null || "".equals(content)) { return; } Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); if (subject != null && !"".equals(subject)) { intent.putExtra(Intent.EXTRA_SUBJECT, subject); } intent.putExtra(Intent.EXTRA_TEXT, content); // 設(shè)置彈出框標(biāo)題 if (dlgTitle != null && !"".equals(dlgTitle)) { // 自定義標(biāo)題 startActivity(Intent.createChooser(intent, dlgTitle)); } else { // 系統(tǒng)默認(rèn)標(biāo)題 startActivity(intent); } }
分享單張圖片
/** * 分享圖片和文字內(nèi)容 * * @param dlgTitle * 分享對話框標(biāo)題 * @param subject * 主題 * @param content * 分享內(nèi)容(文字) * @param uri * 圖片資源URI */ private void shareImg(String dlgTitle, String subject, String content, Uri uri) { if (uri == null) { return; } Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/*"); intent.putExtra(Intent.EXTRA_STREAM, uri); if (subject != null && !"".equals(subject)) { intent.putExtra(Intent.EXTRA_SUBJECT, subject); } if (content != null && !"".equals(content)) { intent.putExtra(Intent.EXTRA_TEXT, content); } // 設(shè)置彈出框標(biāo)題 if (dlgTitle != null && !"".equals(dlgTitle)) { // 自定義標(biāo)題 startActivity(Intent.createChooser(intent, dlgTitle)); } else { // 系統(tǒng)默認(rèn)標(biāo)題 startActivity(intent); } }
分享多張圖片
//分享多張圖片 public void shareMultipleImage(View view) { ArrayList<Uri> uriList = new ArrayList<>(); String path = Environment.getExternalStorageDirectory() + File.separator; uriList.add(Uri.fromFile(new File(path+"australia_1.jpg"))); uriList.add(Uri.fromFile(new File(path+"australia_2.jpg"))); uriList.add(Uri.fromFile(new File(path+"australia_3.jpg"))); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "分享到")); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)基礎(chǔ)之創(chuàng)建啟動界面Splash Screen的方法
這篇文章主要介紹了Android開發(fā)基礎(chǔ)之創(chuàng)建啟動界面Splash Screen的方法,以實例形式較為詳細的分析了Android定制啟動界面的布局及功能實現(xiàn)相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android ScrollView只能添加一個子控件問題解決方法
這篇文章主要介紹了Android ScrollView只能添加一個子控件問題解決方法,涉及Android界面布局的相關(guān)技巧,需要的朋友可以參考下2016-02-02