原文:Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博

兼容SDK 18以上的系统,直接调用系统分享功能,分享文本、图片、文件到第三方APP,如:微信、QQ、微博等

因为偷懒,可直达微信、朋友圈、QQ、QQ空间、微博的分享仅写了图片分享的,其他的文本、文件分享不常用到,就不写了。

具体图片分享区分单张图片分享和多张图片分享,详情请看代码:


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.StrictMode;
import android.text.TextUtils; import java.io.File;
import java.util.ArrayList;
import java.util.List; /**
* 分享文件、图片、文本
* Created by 她叫我小渝 on 2016/10/15.
*/ public class ShareFileUtils { /**
* 分享文本
*
* @param context
* @param path
*/
public static void shareUrl(Context context, String path) {
if (TextUtils.isEmpty(path)) {
return;
} checkFileUriExposure(); Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_TEXT, path);
it.setType("text/plain");
context.startActivity(Intent.createChooser(it, "分享APP"));
} /**
* 分享文件
*
* @param context
* @param path
*/
public static void shareFile(Context context, String path) {
if (TextUtils.isEmpty(path)) {
return;
} checkFileUriExposure(); Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(path))); //传输图片或者文件 采用流的方式
intent.setType("*/*"); //分享文件
context.startActivity(Intent.createChooser(intent, "分享"));
} /**
* 分享单张图片
*
* @param context
* @param path
*/
public static void shareImage(Context context, String path) {
shareImage(context, path, null, null, null);
} /**
* 分享多张图片
*
* @param context
* @param pathList
*/
public static void shareImage(Context context, List<String> pathList) {
shareImage(context, null, pathList, null, null);
} /**
* 分享到微信好友,单图
*/
public static void shareImageToWeChat(Context context, String path) {
//判断是否安装微信,如果没有安装微信 又没有判断就直达微信分享是会挂掉的
if (!isAppInstall(context, "com.tencent.mm")) {
ToastUtils.showToast(context, "您还没有安装微信");
return;
}
shareImage(context, path, null, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
} /**
* 分享到微信好友,多图
*/
public static void shareImageToWeChat(final Context context, List<String> pathList) {
//判断是否安装微信,如果没有安装微信 又没有判断就直达微信分享是会挂掉的
if (!isAppInstall(context, "com.tencent.mm")) {
ToastUtils.showToast(context, "您还没有安装微信");
return;
}
shareImage(context, null, pathList, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
} /**
* 分享到微信朋友圈,单图
*/
public static void shareImageToWeChatFriend(Context context, String path) {
if (!isAppInstall(context, "com.tencent.mm")) {
ToastUtils.showToast(context, "您还没有安装微信");
return;
}
shareImage(context, path, null, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
} /**
* 分享到微信朋友圈,多图
*/
public static void shareImageToWeChatFriend(Context context, List<String> pathList) {
if (!isAppInstall(context, "com.tencent.mm")) {
ToastUtils.showToast(context, "您还没有安装微信");
return;
}
shareImage(context, null, pathList, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
} /**
* 分享图片给QQ好友,单图
*/
public static void shareImageToQQ(Context context, String path) {
if (!isAppInstall(context, "com.tencent.mobileqq")) {
ToastUtils.showToast(context, "您还没有安装QQ");
return;
}
shareImage(context, path, null, "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
} /**
* 分享图片给QQ好友,多图
*/
public static void shareImageToQQ(Context context, List<String> pathList) {
if (!isAppInstall(context, "com.tencent.mobileqq")) {
ToastUtils.showToast(context, "您还没有安装QQ");
return;
}
shareImage(context, null, pathList, "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
} /**
* 分享图片到QQ空间,单图
*/
public static void shareImageToQZone(Context context, String path) {
if (!isAppInstall(context, "com.qzone")) {
ToastUtils.showToast(context, "您还没有安装QQ空间");
return;
}
shareImage(context, path, null, "com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity");
} /**
* 分享图片到QQ空间,多图
*/
public static void shareImageToQZone(Context context, List<String> pathList) {
if (!isAppInstall(context, "com.qzone")) {
ToastUtils.showToast(context, "您还没有安装QQ空间");
return;
}
shareImage(context, null, pathList, "com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity");
} /**
* 分享图片到微博,单图
*/
public static void shareImageToWeibo(Context context, String path) {
if (!isAppInstall(context, "com.sina.weibo")) {
ToastUtils.showToast(context, "您还没有安装新浪微博");
return;
}
shareImage(context, path, null, "com.sina.weibo", "com.sina.weibo.EditActivity");
} /**
* 分享图片到微博,多图
*/
public static void shareImageToWeibo(Context context, List<String> pathList) {
if (!isAppInstall(context, "com.sina.weibo")) {
ToastUtils.showToast(context, "您还没有安装新浪微博");
return;
}
shareImage(context, null, pathList, "com.sina.weibo", "com.sina.weibo.EditActivity");
} /**
* 检测手机是否安装某个应用
*
* @param context
* @param appPackageName 应用包名
* @return true-安装,false-未安装
*/
public static boolean isAppInstall(Context context, String appPackageName) {
PackageManager packageManager = context.getPackageManager();// 获取packagemanager
List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
if (pinfo != null) {
for (int i = 0; i < pinfo.size(); i++) {
String pn = pinfo.get(i).packageName;
if (appPackageName.equals(pn)) {
return true;
}
}
}
return false;
} /**
* 分享前必须执行本代码,主要用于兼容SDK18以上的系统
*/
private static void checkFileUriExposure() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
}
} /**
* @param context 上下文
* @param path 不为空的时候,表示分享单张图片,会检验图片文件是否存在
* @param pathList 不为空的时候表示分享多张图片,会检验每一张图片是否存在
* @param pkg 分享到的指定app的包名
* @param cls 分享到的页面(微博不需要指定页面)
*/
private static void shareImage(Context context, String path, List<String> pathList, String pkg, String cls) {
if (path == null && pathList == null) {
ToastUtils.showToast(context, "找不到您要分享的图片文件");
return;
} checkFileUriExposure(); try {
if (path != null) {
//单张图片
if (!MyFileUtils.isFile(path)) {
ToastUtils.showToast(context, "图片不存在,请检查后重试");
return;
} Intent intent = new Intent();
if (pkg != null && cls != null) {
//指定分享到的app
if (pkg.equals("com.sina.weibo")) {
//微博分享的需要特殊处理
intent.setPackage(pkg);
} else {
ComponentName comp = new ComponentName(pkg, cls);
intent.setComponent(comp);
}
}
intent.setAction(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(path)));
intent.setType("image/*"); //分享文件
context.startActivity(Intent.createChooser(intent, "分享"));
} else {
//多张图片
ArrayList<Uri> uriList = new ArrayList<>();
for (int i = 0; i < pathList.size(); i++) {
if (!MyFileUtils.isFile(pathList.get(i))) {
ToastUtils.showToast(context, "第" + (i + 1) + "张图片不存在,请检查后重试");
return;
}
uriList.add(Uri.fromFile(new File(pathList.get(i))));
} Intent intent = new Intent(); if (pkg != null && cls != null) {
//指定分享到的app
if (pkg.equals("com.sina.weibo")) {
//微博分享的需要特殊处理
intent.setPackage(pkg);
} else {
ComponentName comp = new ComponentName(pkg, cls);
intent.setComponent(comp);
}
}
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType("image/*");
context.startActivity(Intent.createChooser(intent, "分享"));
} } catch (Exception e) {
ToastUtils.showToast(context, "分享失败,未知错误");
}
} }

 

Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博的更多相关文章

  1. Android NineGridLayout — 仿微信朋友圈和QQ空间的九宫格图片展示自定义控件

    NineGridLayout 一个仿微信朋友圈和QQ空间的九宫格图片展示自定义控件. GitHub:https://github.com/HMY314/NineGridLayout 一.介绍 1.当只 ...

  2. android - 调用系统分享功能分享图片

    step1: 编写分享代码, 将Uri的生成方式改为由FileProvider提供的临时授权路径,并且在intent中添加flag 注意:在Android7.0之后,调用系统分享,传入URI的时候可能 ...

  3. Android调用系统分享功能总结

    Android分享-调用系统自带的分享功能 实现分享功能的几个办法 1.调用系统的分享功能 2.通过第三方SDK,如ShareSDK,友盟等 3.自行使用各自平台的SDK,比如QQ,微信,微博各自的S ...

  4. Android调用系统分享功能以及createChooser的使用

    工程结构 //效果图 点击测试分享                                                                                   ...

  5. 实现手机网页调起原生微信朋友圈分享的工具nativeShare.js

    http://www.liaoxiansheng.cn/?p=294 我们知道现在我们无法直接通过js直接跳转到微信和QQ等软件进行分享,但是现在像UC浏览器和QQ浏览器这样的主流浏览器自带一个分享工 ...

  6. 探索Android调用系统的分享功能

    非常多的应用为了应用的推广和传播都会使用"分享"的功能,点击分享button.就能将想要分享的内容或者图片分享至QQ空间.微博.微信朋友圈等实现了分享功能的应用.这篇文章主要是为了 ...

  7. (转)Android调用系统自带的文件管理器进行文件选择并获得路径

    Android区别于iOS的沙盒模式,可以通过文件浏览器浏览本地的存储器.Android API也提供了相应的接口. 基本思路,先通过Android API调用系统自带的文件浏览器选取文件获得URI, ...

  8. Android调用系统相机、自己定义相机、处理大图片

    Android调用系统相机和自己定义相机实例 本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,而且因为涉及到要把拍到的照片显示出来,该样例也会涉及到Android载入大图片时候的处 ...

  9. android 7.0以上共享文件(解决调用系统照相和图片剪切出现的FileUriExposedException崩溃问题)

    在android7.0开始试共享“file://”URI 将会导致引发 FileUriExposedException. 如果应用需要与其他应用共享私有文件,则应该使用 FileProvider, F ...

随机推荐

  1. 基于PHP实现一个简单的在线聊天功能(轮询ajax )

    基于PHP实现一个简单的在线聊天功能(轮询ajax ) 一.总结 1.用的轮询ajax 二.基于PHP实现一个简单的在线聊天功能 一直很想试着做一做这个有意思的功能,感觉复杂的不是数据交互和表结构,麻 ...

  2. Caffe 安装 Ubuntu14.04+CUDA7.0/7.5(亲测有效)

    自己安装Caffe的过程中,参考了很多资料,但由于版本或者其他原因,安装过程中总是遇到这样或者那样的问题,因此留做记录,方便之后查看,也希望对遇到相似麻烦的朋友们提供帮助.  下面我们开始安装吧: 硬 ...

  3. POJ 1065 Wooden Sticks(zoj 1025) 最长单调子序列

    POJ :http://poj.org/problem?id=1065 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId= ...

  4. matplotlib学习之散点图与条形图

    # coding:utf-8 from matplotlib import pyplot as plt import numpy as np plt.style.use('ggplot') x = n ...

  5. surfingkeys

    https://www.appinn.com/surfingkeys-for-chrome/ 尝试使用.听说能支持js Vimium 不支持拷贝链接的文本. 不支持stop page https:// ...

  6. java异常——捕获异常+再次抛出异常与异常链

    [0]README 0.1) 本文描述+源代码均 转自 core java volume 1, 旨在理解 java异常--捕获异常+再次抛出异常与异常链 的相关知识: [1]捕获异常相关 1.1)如果 ...

  7. [Compose] 14. Build curried functions

    We see what it means to curry a function, then walk through several examples of curried functions an ...

  8. Cocos2d-x 3.0final 终结者系列教程05-AppDelegate入口类

    下面是Cocos2d-x的程序入口: class  AppDelegate : private cocos2d::Application { public: AppDelegate(); virtua ...

  9. 从多路搜索树到 B-树

    1. 什么是 B 树 B 树是为磁盘或其他直接存取的辅助存储设备而设计的一种平衡二叉树: B 树类似于红黑树,但它们在降低磁盘 I/O 操作数方面要更好一点, 许多数据库系统使用 B 树或者 B 树的 ...

  10. Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty

    原文:Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty public class NullToEmptyStringResolver : De ...