原文: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. C API函数描写叙述(S-W)

    25.2.3.59. mysql_select_db() int mysql_select_db(MYSQL *mysql, const char *db) 描写叙述 使由db指定的数据库成为由mys ...

  2. SQL日期时间函数

    一.Sql Server中的日期与时间函数 1. 当前系统日期.时间  select getdate() 2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值  例如 ...

  3. 3、linux下Socket编程-TCP/UDP

    1.什么是Socket 网络的 Socket数据传输是一种特殊的I/O,Socket也是一种文件描述符.Socket也具有一个类似于打开文件的函数调用Socket(),该函数返 回一个整型的Socke ...

  4. ios开发零散知识点总结

    1:当有导航栏的时候,子视图为UIScrollView,或是继承于UIScrollView的控件如UITableView,UICollectionView等,控制器会自动调用 self.automat ...

  5. 【编程】常见概念的理解 —— inplace、vanity url、vanilla(code/software)、编译、链接、build、(delegate、proxy)

    inplace: 如修改一个文件等对象时, inplace=True,不创建新的对象,直接在原始对象上尽心修改: inplace=False,在对原始对象进行修改,而会创建新的对象: vanity u ...

  6. [Django] Auth django app with rest api

    First, start the env: . bin/activate Then cd to our module cd djangular Create a new app: python man ...

  7. css选择器指定元素中第几个子元素

    tr td:nth-child(2){ background-color:gray; } 就是tr当中的td的第二个td的属性 tr:nth-child(2n+0){ background-color ...

  8. 三天打渔,俩天晒网(C++实现)

    #include <iostream> using namespace std; int leap (int a) {     if (a%4==0%a%100!=0||a%400==0) ...

  9. C++ 指向类成员函数指针的用法(转自维基百科)

    类成员函数指针 类成员函数指针(member function pointer),是C++语言的一类指针数据类型,用于存储一个指定类具有给定的形参列表与返回值类型的成员函数的访问信息. 目录 1 语法 ...

  10. erlang分布式入门(一)-ping pong

    erlang分布式入门(一)-ping pong 测试环境和http://willvvv.iteye.com/blog/1523918 一样,192.168.0.182(centos-182)和192 ...