系统分享

作者: 才兄说 | 来源:发表于2017-06-11 14:57 被阅读43次

前言:分享分两大类,一类是系统分享,简单直接;一类是三方分享,功能集成更加完善强大

下面分享一个写的关于系统分享类型的工具类:
(其中测试过的是分享pdf,文本,图片等,如需补充或使用遇到问题可以及时反馈,检查修正后及时更新)

public class SysShareUtil {

    private final String SHARE_PANEL_TITLE = "分享到";//分享面板标题
    //分享pdf文件
    public void sharePdf(Context context, String filePath) {
        Uri fileUri = Uri.fromFile(new File(filePath));
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
        shareIntent.setType("application/pdf");
        context.startActivity(Intent.createChooser(shareIntent, SHARE_PANEL_TITLE));
    }
    //分享文字
    public void shareText(Context context, String shareText) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");//适配该类型的应用会出现在分享面板上
        shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
        context.startActivity(Intent.createChooser(shareIntent, SHARE_PANEL_TITLE));
    }
    //分享单张图片
    public void shareSingleImage(Context context, String imagePath) {
        //由文件得到uri
        Uri imageUri = Uri.fromFile(new File(imagePath));

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("image/*");
        context.startActivity(Intent.createChooser(shareIntent, SHARE_PANEL_TITLE));
    }
    //分享多张图片
    public void shareMultipleImage(Context context, List<String> imagePaths) {
        ArrayList<Uri> uriList = new ArrayList<>();
        for (int i = 0; i < imagePaths.size(); i++) {
            String imagePath = imagePaths.get(i);
            Uri imageUri = Uri.fromFile(new File(imagePath));
            uriList.add(imageUri);
        }
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
        shareIntent.setType("image/*");
        context.startActivity(Intent.createChooser(shareIntent, SHARE_PANEL_TITLE));
    }
    /**
     * 分享图文
     *
     * @param context       上下文
     * @param activityTitle Activity的名字
     * @param msgTitle      消息标题
     * @param msgText       消息内容
     * @param imgPath       图片路径,不分享图片则传null
     */
    public void shareImageText(Context context, String activityTitle, String msgTitle, String msgText,
                               String imgPath) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        if (imgPath == null || imgPath.equals("")) {
            intent.setType("text/plain"); // 纯文本
        } else {
            File f = new File(imgPath);
            if (f != null && f.exists() && f.isFile()) {
                intent.setType("image/jpg");
                Uri u = Uri.fromFile(f);
                intent.putExtra(Intent.EXTRA_STREAM, u);
            }
        }
        intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
        intent.putExtra(Intent.EXTRA_TEXT, msgText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(intent, activityTitle));
    }
    private static SysShareUtil instance;
    private SysShareUtil() {
    }
    //唯一实例入口
    public static SysShareUtil getInstance() {
        if (null == instance) {
            synchronized (SysShareUtil.class) {
                if (null == instance) {
                    instance = new SysShareUtil();
                }
            }
        }
        return instance;
    }

}

设置应用在系统分享时过滤匹配的类型:

<intent-filter>  
     <action android:name="android.intent.action.SEND" />  
  
     <category android:name="android.intent.category.DEFAULT" />  
  
     <data android:mimeType="*/*" />  
 </intent-filter>  

补充:
1、如上android:mimeType就是设置过滤类型,这里的语法是匹配所有的分享类型
2、如下是支持的MIME类型:

{".3gp", "video/3gpp"},  
{".apk", "application/vnd.android.package-archive"},  
{".asf", "video/x-ms-asf"},  
{".avi", "video/x-msvideo"},  
{".bin", "application/octet-stream"},  
{".bmp", "image/bmp"},  
{".c", "text/plain"},  
{".class", "application/octet-stream"},  
{".conf", "text/plain"},  
{".cpp", "text/plain"},  
{".doc", "application/msword"},  
{".exe", "application/octet-stream"},  
{".gif", "image/gif"},  
{".gtar", "application/x-gtar"},  
{".gz", "application/x-gzip"},  
{".h", "text/plain"},  
{".htm", "text/html"},  
{".html", "text/html"},  
{".jar", "application/java-archive"},  
{".java", "text/plain"},  
{".jpeg", "image/jpeg"},  
{".jpg", "image/jpeg"},  
{".js", "application/x-javascript"},  
{".log", "text/plain"},  
{".m3u", "audio/x-mpegurl"},  
{".m4a", "audio/mp4a-latm"},  
{".m4b", "audio/mp4a-latm"},  
{".m4p", "audio/mp4a-latm"},  
{".m4u", "video/vnd.mpegurl"},  
{".m4v", "video/x-m4v"},  
{".mov", "video/quicktime"},  
{".mp2", "audio/x-mpeg"},  
{".mp3", "audio/x-mpeg"},  
{".mp4", "video/mp4"},  
{".mpc", "application/vnd.mpohun.certificate"},  
{".mpe", "video/mpeg"},  
{".mpeg", "video/mpeg"},  
{".mpg", "video/mpeg"},  
{".mpg4", "video/mp4"},  
{".mpga", "audio/mpeg"},  
{".msg", "application/vnd.ms-outlook"},  
{".ogg", "audio/ogg"},  
{".pdf", "application/pdf"},  
{".png", "image/png"},  
{".pps", "application/vnd.ms-powerpoint"},  
{".ppt", "application/vnd.ms-powerpoint"},  
{".prop", "text/plain"},  
{".rar", "application/x-rar-compressed"},  
{".rc", "text/plain"},  
{".rmvb", "audio/x-pn-realaudio"},  
{".rtf", "application/rtf"},  
{".sh", "text/plain"},  
{".tar", "application/x-tar"},  
{".tgz", "application/x-compressed"},  
{".txt", "text/plain"},  
{".wav", "audio/x-wav"},  
{".wma", "audio/x-ms-wma"},  
{".wmv", "audio/x-ms-wmv"},  
{".wps", "application/vnd.ms-works"},  
{".xml", "text/xml"},  
{".xml", "text/plain"},  
{".z", "application/x-compress"},  
{".zip", "application/zip"},  
{"", "*/*"} 

相关文章

  • 日精进;——176天!

    系统 系统 系统 开会 开会 开会 分享 分享 分享 简单 简单 简单 智慧 智慧 智慧 复习 复习 复习全员发 ...

  • Android开发之分享功能(一)

    1.实现系统自带的分享功能 使用android系统提供的分享功能:点击分享按钮,系统会自动获取手机内提供分享功能的...

  • 系统自带的分享

    系统自带分享使用 "框架 系统自带分享分为三步 1.判断分享是否可用 [SLComposeViewControll...

  • 苹果系统自带的分享

    系统自带分享使用 #import" "框架 参考链接 系统自带分享分为三步: 1.判断分享是否可用 if (![S...

  • 系统分享

    //调用系统分享代码 sit(a, "action", "android.intent.action.SEND")...

  • 系统分享

    前言:分享分两大类,一类是系统分享,简单直接;一类是三方分享,功能集成更加完善强大 下面分享一个写的关于系统分享类...

  • #系统排列分享

    有的时候我很难理解这些专家的概念,家庭这样一个“组织”在现代社会展现的姿态是五花八门,问题也是千种万种,在细节方面...

  • #系统排列分享

    朋友圈的分享 《谁在我家》 让过去的事情成为过去,让将来的事情自然出现,这本身蕴藏着自然界的和谐。 一棵大树上面的...

  • 系统排列分享

    知识一点点积累,看到有兴趣的东西就喜欢收藏好。 #系统排列分享 当我们跟家庭有很深联结的时候,一种深层的平静就会出...

  • 系统排列分享

    #系统排列分享 解答 一学生询问他的老师:“请告诉我,自由是什么?” 老师问:“什么样的自由?第一种自由是愚昧。就...

网友评论

    本文标题:系统分享

    本文链接:https://www.haomeiwen.com/subject/cqmhqxtx.html