原文: Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博
兼容SDK 18以上的系统,直接调用系统分享功能,分享文本、图片、文件到第三方APP,如:微信、QQ、微博等
因为偷懒,可直达微信、朋友圈、QQ、QQ空间、微博的分享仅写了图片分享的,其他的文本、文件分享不常用到,就不写了。
具体图片分享区分单张图片分享和多张图片分享,详情请看代码:
1import android.content.ComponentName; 2import android.content.Context; 3import android.content.Intent; 4import android.content.pm.PackageInfo; 5import android.content.pm.PackageManager; 6import android.net.Uri; 7import android.os.Build; 8import android.os.StrictMode; 9import android.text.TextUtils; 10 11import java.io.File; 12import java.util.ArrayList; 13import java.util.List; 14 15/** 16 * 分享文件、图片、文本 17 * Created by 她叫我小渝 on 2016/10/15. 18 */ 19 20public class ShareFileUtils { 21 22 /** 23 * 分享文本 24 * 25 * @param context 26 * @param path 27 */ 28 public static void shareUrl(Context context, String path) { 29 if (TextUtils.isEmpty(path)) { 30 return; 31 } 32 33 checkFileUriExposure(); 34 35 Intent it = new Intent(Intent.ACTION_SEND); 36 it.putExtra(Intent.EXTRA_TEXT, path); 37 it.setType("text/plain"); 38 context.startActivity(Intent.createChooser(it, "分享APP")); 39 } 40 41 /** 42 * 分享文件 43 * 44 * @param context 45 * @param path 46 */ 47 public static void shareFile(Context context, String path) { 48 if (TextUtils.isEmpty(path)) { 49 return; 50 } 51 52 checkFileUriExposure(); 53 54 Intent intent = new Intent(Intent.ACTION_SEND); 55 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 56 intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(path))); //传输图片或者文件 采用流的方式 57 intent.setType("*/*"); //分享文件 58 context.startActivity(Intent.createChooser(intent, "分享")); 59 } 60 61 /** 62 * 分享单张图片 63 * 64 * @param context 65 * @param path 66 */ 67 public static void shareImage(Context context, String path) { 68 shareImage(context, path, null, null, null); 69 } 70 71 /** 72 * 分享多张图片 73 * 74 * @param context 75 * @param pathList 76 */ 77 public static void shareImage(Context context, List<String> pathList) { 78 shareImage(context, null, pathList, null, null); 79 } 80 81 /** 82 * 分享到微信好友,单图 83 */ 84 public static void shareImageToWeChat(Context context, String path) { 85 //判断是否安装微信,如果没有安装微信 又没有判断就直达微信分享是会挂掉的 86 if (!isAppInstall(context, "com.tencent.mm")) { 87 ToastUtils.showToast(context, "您还没有安装微信"); 88 return; 89 } 90 shareImage(context, path, null, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI"); 91 } 92 93 /** 94 * 分享到微信好友,多图 95 */ 96 public static void shareImageToWeChat(final Context context, List<String> pathList) { 97 //判断是否安装微信,如果没有安装微信 又没有判断就直达微信分享是会挂掉的 98 if (!isAppInstall(context, "com.tencent.mm")) { 99 ToastUtils.showToast(context, "您还没有安装微信"); 100 return; 101 } 102 shareImage(context, null, pathList, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI"); 103 } 104 105 /** 106 * 分享到微信朋友圈,单图 107 */ 108 public static void shareImageToWeChatFriend(Context context, String path) { 109 if (!isAppInstall(context, "com.tencent.mm")) { 110 ToastUtils.showToast(context, "您还没有安装微信"); 111 return; 112 } 113 shareImage(context, path, null, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI"); 114 } 115 116 /** 117 * 分享到微信朋友圈,多图 118 */ 119 public static void shareImageToWeChatFriend(Context context, List<String> pathList) { 120 if (!isAppInstall(context, "com.tencent.mm")) { 121 ToastUtils.showToast(context, "您还没有安装微信"); 122 return; 123 } 124 shareImage(context, null, pathList, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI"); 125 } 126 127 /** 128 * 分享图片给QQ好友,单图 129 */ 130 public static void shareImageToQQ(Context context, String path) { 131 if (!isAppInstall(context, "com.tencent.mobileqq")) { 132 ToastUtils.showToast(context, "您还没有安装QQ"); 133 return; 134 } 135 shareImage(context, path, null, "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity"); 136 } 137 138 139 /** 140 * 分享图片给QQ好友,多图 141 */ 142 public static void shareImageToQQ(Context context, List<String> pathList) { 143 if (!isAppInstall(context, "com.tencent.mobileqq")) { 144 ToastUtils.showToast(context, "您还没有安装QQ"); 145 return; 146 } 147 shareImage(context, null, pathList, "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity"); 148 } 149 150 151 /** 152 * 分享图片到QQ空间,单图 153 */ 154 public static void shareImageToQZone(Context context, String path) { 155 if (!isAppInstall(context, "com.qzone")) { 156 ToastUtils.showToast(context, "您还没有安装QQ空间"); 157 return; 158 } 159 shareImage(context, path, null, "com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity"); 160 } 161 162 163 /** 164 * 分享图片到QQ空间,多图 165 */ 166 public static void shareImageToQZone(Context context, List<String> pathList) { 167 if (!isAppInstall(context, "com.qzone")) { 168 ToastUtils.showToast(context, "您还没有安装QQ空间"); 169 return; 170 } 171 shareImage(context, null, pathList, "com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity"); 172 } 173 174 /** 175 * 分享图片到微博,单图 176 */ 177 public static void shareImageToWeibo(Context context, String path) { 178 if (!isAppInstall(context, "com.sina.weibo")) { 179 ToastUtils.showToast(context, "您还没有安装新浪微博"); 180 return; 181 } 182 shareImage(context, path, null, "com.sina.weibo", "com.sina.weibo.EditActivity"); 183 } 184 185 186 /** 187 * 分享图片到微博,多图 188 */ 189 public static void shareImageToWeibo(Context context, List<String> pathList) { 190 if (!isAppInstall(context, "com.sina.weibo")) { 191 ToastUtils.showToast(context, "您还没有安装新浪微博"); 192 return; 193 } 194 shareImage(context, null, pathList, "com.sina.weibo", "com.sina.weibo.EditActivity"); 195 } 196 197 /** 198 * 检测手机是否安装某个应用 199 * 200 * @param context 201 * @param appPackageName 应用包名 202 * @return true-安装,false-未安装 203 */ 204 public static boolean isAppInstall(Context context, String appPackageName) { 205 PackageManager packageManager = context.getPackageManager();// 获取packagemanager 206 List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息 207 if (pinfo != null) { 208 for (int i = 0; i < pinfo.size(); i++) { 209 String pn = pinfo.get(i).packageName; 210 if (appPackageName.equals(pn)) { 211 return true; 212 } 213 } 214 } 215 return false; 216 } 217 218 /** 219 * 分享前必须执行本代码,主要用于兼容SDK18以上的系统 220 */ 221 private static void checkFileUriExposure() { 222 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 223 StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); 224 StrictMode.setVmPolicy(builder.build()); 225 builder.detectFileUriExposure(); 226 } 227 } 228 229 /** 230 * @param context 上下文 231 * @param path 不为空的时候,表示分享单张图片,会检验图片文件是否存在 232 * @param pathList 不为空的时候表示分享多张图片,会检验每一张图片是否存在 233 * @param pkg 分享到的指定app的包名 234 * @param cls 分享到的页面(微博不需要指定页面) 235 */ 236 private static void shareImage(Context context, String path, List<String> pathList, String pkg, String cls) { 237 if (path == null && pathList == null) { 238 ToastUtils.showToast(context, "找不到您要分享的图片文件"); 239 return; 240 } 241 242 checkFileUriExposure(); 243 244 try { 245 if (path != null) { 246 //单张图片 247 if (!MyFileUtils.isFile(path)) { 248 ToastUtils.showToast(context, "图片不存在,请检查后重试"); 249 return; 250 } 251 252 Intent intent = new Intent(); 253 if (pkg != null && cls != null) { 254 //指定分享到的app 255 if (pkg.equals("com.sina.weibo")) { 256 //微博分享的需要特殊处理 257 intent.setPackage(pkg); 258 } else { 259 ComponentName comp = new ComponentName(pkg, cls); 260 intent.setComponent(comp); 261 } 262 } 263 intent.setAction(Intent.ACTION_SEND); 264 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 265 intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(path))); 266 intent.setType("image/*"); //分享文件 267 context.startActivity(Intent.createChooser(intent, "分享")); 268 } else { 269 //多张图片 270 ArrayList<Uri> uriList = new ArrayList<>(); 271 for (int i = 0; i < pathList.size(); i++) { 272 if (!MyFileUtils.isFile(pathList.get(i))) { 273 ToastUtils.showToast(context, "第" + (i + 1) + "张图片不存在,请检查后重试"); 274 return; 275 } 276 uriList.add(Uri.fromFile(new File(pathList.get(i)))); 277 } 278 279 Intent intent = new Intent(); 280 281 if (pkg != null && cls != null) { 282 //指定分享到的app 283 if (pkg.equals("com.sina.weibo")) { 284 //微博分享的需要特殊处理 285 intent.setPackage(pkg); 286 } else { 287 ComponentName comp = new ComponentName(pkg, cls); 288 intent.setComponent(comp); 289 } 290 } 291 intent.setAction(Intent.ACTION_SEND_MULTIPLE); 292 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList); 293 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 294 intent.setType("image/*"); 295 context.startActivity(Intent.createChooser(intent, "分享")); 296 } 297 298 } catch (Exception e) { 299 ToastUtils.showToast(context, "分享失败,未知错误"); 300 } 301 } 302 303}