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

原文: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, "分享失败,未知错误");
        }
    }

}

?

原文地址:https://www.cnblogs.com/lonelyxmas/p/9855721.html

时间: 2024-08-06 20:45:29

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

android 调用系统分享图片及文字

调用系统分享文字:public static void shareText(Context context, String extraText) {Intent intent = new Intent(Intent.ACTION_SEND);intent.setType("text/plain");intent.putExtra(Intent.EXTRA_SUBJECT, "连接分享");intent.putExtra(Intent.EXTRA_TEXT, extr

android 调用系统相机获取图片、调用系统相册获取图片,并对图片进行截取

打开系统相册获取图片并截取,代码相对简单 1 Intent intent = new Intent(Intent.ACTION_GET_CONTENT,null); 2 intent.setType("image/*"); 3 intent.putExtra("crop", "true"); 4 5 //WIDTH 和 HEIGHT指的是截取框的宽高比例,如设WIDTH = 1,HEIGHT = 1,截取框就为正方形 6 intent.putEx

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

NineGridLayout 一个仿微信朋友圈和QQ空间的九宫格图片展示自定义控件. GitHub:https://github.com/HMY314/NineGridLayout 一.介绍 1.当只有1张图时,可以自己定制图片宽高,也可以使用默认九宫格的宽高: 2.当只有4张图时,以2*2的方式显示: 3.除以上两种情况下,都是按照3列方式显示,但这时有一些细节: a.如果只有9张图,当然是以3*3的方式显示: b.如果超过9张图,可以设置是否全部显示. 如果设置不完全显示,则按照3*3的方式

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

step1: 编写分享代码, 将Uri的生成方式改为由FileProvider提供的临时授权路径,并且在intent中添加flag 注意:在Android7.0之后,调用系统分享,传入URI的时候可能会导致程序闪退崩溃.这是由于7.0的新的文件权限导致的.下面的代码对其做了处理 public static int sharePic(Context context, String picFilePath) { File shareFile = new File(picFilePath); if (

android 滚动条下拉反弹的效果(类似微信朋友圈)

微信朋友圈上面的图片封面,QQ空间说说上面的图片封面都有下拉反弹的效果,这些都是使用滚动条实现的.下拉,当松开时候,反弹至原来的位置.下拉时候能看到背景图片.那么这里简单介绍一下这种效果的实现. 本文源码下载:点击 1.效果图 这部手机显示的分辨率有限,很老的手机调试. 2.具有反弹效果BounceScrollView package com.org.scroll; import android.content.Context; import android.graphics.Rect; imp

Android调用系统分享分享内容到其他应用,不使用系统的ActionBar的弹窗,完全自定义

Android分享内容到其他应用,调用系统的Dialog或者ActionBar的弹窗都不太自由,限制太大,这里我提供一个完全自定界面的,可以弹窗,也可以直接在Activity或者Fragment里边自定义界面.这里展示一个关键类的代码,做了封装处理,我写两个一个Demo,免费源码在:http://download.csdn.net/detail/yanzhenjie1003/8565449 /** * @author YOLANDA * @Time 2015年4月5日 下午1:03:11 */

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

工程结构 //效果图 点击测试分享                                                                                                          点击createChoose妙用       主要是看右边的,可不是用什么Dialog来搞的哦,而是你Activity程序,可以激活进去了 提示:这个东西可以延伸到一个音频文件,打开时,可以调用你的音乐播放器来播放哦,视频,图片,也是类似,可以调用你自己

Android 修改源码自定义SwipeRefreshLayout样式——高仿微信朋友圈下拉刷新

修改源码自定义SwipeRefreshLayout样式——高仿微信朋友圈下拉刷新 原文地址:https://www.cnblogs.com/zhujiabin/p/8194996.html

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

http://www.liaoxiansheng.cn/?p=294 我们知道现在我们无法直接通过js直接跳转到微信和QQ等软件进行分享,但是现在像UC浏览器和QQ浏览器这样的主流浏览器自带一个分享工具,而他们也有自己定义的js接口.我们通过调用浏览器的接口去调用浏览器的分享,从而实现原生分享功能.是不是很酷呢? 工具介绍: nativeShare是一个可以通过javascript直接调用原生分享的工具. 该工具具有以下特点: 支持原生微博.微信好友.微信朋友圈.QQ好友.QQ空间分享 支持调用