android qq图片分享到朋友圈,Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博...
转载自:https://www.liangzl.com/get-article-detail-18749.html
兼容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 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 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 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 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 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 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 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 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 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图片分享到朋友圈,Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博...相关推荐
- Android调用系统分享和指定app分享-微信朋友圈图文分享和qq分享
Android调用系统分享和指定app分享-微信朋友圈图文分享和qq分享 标签: Android系统分享QQ分享朋友圈图文分享 2016-09-27 22:54 279人阅读 评论(0) 收藏 举报 ...
- Android 调用系统分享图片、视频等文件
目录 序言 打开文件管理器 分享操作 文件Uri转化为String 序言 一般都会集成Umeng的分享,基本的分享都可以满足,但是要想分享文件,比如pdf.doc.ppt等类型时,umeng就不能满足 ...
- android - 调用系统分享功能分享图片
step1: 编写分享代码, 将Uri的生成方式改为由FileProvider提供的临时授权路径,并且在intent中添加flag 注意:在Android7.0之后,调用系统分享,传入URI的时候可能 ...
- android 调用系统分享到微信,Android调用系统分享直接抵达微信
前言 相对于使用微信官方的分享sdk,系统调用的分享到微信还是有很大的差异的. 系统分享而微信官方sdk分享的最大区别就是关于朋友圈分享,微信朋友圈对系统分享的限制是只允许分享照片.而对分享给联系人, ...
- Android开发 调用系统相机相册图片功能,解决小米手机拍照或者图片横竖相反问题,及小米手机相册图片路径问题
Android开发 调用系统相机相册图片功能,解决小米手机拍照或者图片横竖相反问题,及小米手机相册图片路径问题 1.调用相机,兼容7.0 AndroidManifest配置 <providera ...
- Android开发:调用系统图库选择图片
方法: //调用系统图库选择图片 public Intent selectPicture() {Intent intent = new Intent(Intent.ACTION_PICK,androi ...
- Android之调用系统分享
1.调用系统分享关键代码 private void shareImage() {Intent intent = new Intent(Intent.ACTION_SEND); intent.setFl ...
- android调用系统下载器下载文件
在项目中使用到了在android端下载服务端的文件,之前使用的是http下载方式,现在想改成调用系统的下载器进行下载,实现步骤为: 1.得到url:文件下载地址 2.使用URL下载: (1)下载前先判 ...
- 随笔记录2、Android端调用系统分享文件记录
Android端调用系统发送文件 效果如如下: Android 7.0以上需要适配 第一步: 1. AndroidManifest.xml中注册.provider 2. android:resourc ...
最新文章
- yii2 引入php文件,Yii2中YiiBase自动加载类、引用文件方法分析(autoload)
- 9.2 图像类型(灰度图像、彩色图像、二值图像以及图像间的转换)
- Java Review - 并发编程_抽象同步队列AQS
- python微信加人_Python帮你微信头像任意添加装饰,别再@微信官方了_编程语言_python考试视频_python教程_课课家...
- B. Lynyrd Skynyrd(倍增 + 区间最小值)
- 下列关于html5表单的多样输入方式,IT兄弟连 HTML5教程 HTML5表单 多样的输入类型1...
- 血型算法php,血型排行榜!(真的很准)
- java基础----多态性
- bzoj 1414 bzoj 3705: [ZJOI2009]对称的正方形(二维Hash)
- 关于对QQ 输入法的评价
- 大数据平台--调度系统
- css 字体压缩 fonttools
- linux下安装php+apache+mysql集成环境
- 163邮箱如何注册呢?
- java根据出生日期计算年龄_通过出生日期获取年龄的方法--Java
- 第三章(第一部分) 月夜猫の魅 友谊的决裂
- deepin efi 启动u盘_deepin启动引导修复教程
- python爬虫excel数据_最简单的爬数据方法:Excel爬取数据,仅需6步
- 罗马数字和阿拉伯数字之间的转换
- 仿QQ空间图片放缩查看