Android系统自带分享功能的实现(可同时分享文字和图片)

简单,不解释,直接上代码,可直接使用!

代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /** 
  2.      * 分享功能 
  3.      *  
  4.      * @param context 
  5.      *            上下文 
  6.      * @param activityTitle 
  7.      *            Activity的名字 
  8.      * @param msgTitle 
  9.      *            消息标题 
  10.      * @param msgText 
  11.      *            消息内容 
  12.      * @param imgPath 
  13.      *            图片路径,不分享图片则传null 
  14.      */  
  15.     public void shareMsg(String activityTitle, String msgTitle, String msgText,  
  16.             String imgPath) {  
  17.         Intent intent = new Intent(Intent.ACTION_SEND);  
  18.         if (imgPath == null || imgPath.equals("")) {  
  19.             intent.setType("text/plain"); // 纯文本  
  20.         } else {  
  21.             File f = new File(imgPath);  
  22.             if (f != null && f.exists() && f.isFile()) {  
  23.                 intent.setType("image/jpg");  
  24.                 Uri u = Uri.fromFile(f);  
  25.                 intent.putExtra(Intent.EXTRA_STREAM, u);  
  26.             }  
  27.         }  
  28.         intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);  
  29.         intent.putExtra(Intent.EXTRA_TEXT, msgText);  
  30.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  31.         startActivity(Intent.createChooser(intent, activityTitle));  
  32.     }  
原文地址:https://www.cnblogs.com/xgjblog/p/4368677.html