系统分享

分享分两类:系统分享、三方分享(功能更完善强大)

弹起系统分享面板:

//分享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, "分享到"));//面板标题
}

 

只要在清单文件中配置了如下过滤,并且类型匹配,在弹起系统分享面板时,我们的应用也会出现在面板中:

<activity
    android:name="com.example.huangweicai.androidsysshare.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />


        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>


    <!--配置了该过滤,并且匹配mimeType类型,就可以在系统分享的面板上出现-->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <!-- 指定分享类型,匹配被请求弹起的分享面板类型 -->
        <data android:mimeType="application/pdf"/>
        <!--<data android:mimeType="video/mp4"/>-->
    </intent-filter>
</activity>

补充:

清单文件中匹配任何类型:

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

支持的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"},  
  {"", "*/*"}  

系统分享工具类:

package com.example.huangweicai.androidsysshare;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;


import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * 创建时间:2017/5/17
 * 编写者:黄伟才
 * 功能描述:系统分享工具类
 */


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;
    }


}

 

发布了86 篇原创文章 · 获赞 5 · 访问量 3万+
App 阅读领勋章
微信扫码 下载APP
阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览