系統(tǒng)分享

前言:分享分兩大類(lèi),一類(lèi)是系統(tǒng)分享,簡(jiǎn)單直接;一類(lèi)是三方分享,功能集成更加完善強(qiáng)大

下面分享一個(gè)寫(xiě)的關(guān)于系統(tǒng)分享類(lèi)型的工具類(lèi):
(其中測(cè)試過(guò)的是分享pdf,文本,圖片等,如需補(bǔ)充或使用遇到問(wèn)題可以及時(shí)反饋,檢查修正后及時(shí)更新)

public class SysShareUtil {

    private final String SHARE_PANEL_TITLE = "分享到";//分享面板標(biāo)題
    //分享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");//適配該類(lèi)型的應(yīng)用會(huì)出現(xiàn)在分享面板上
        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      消息標(biāo)題
     * @param msgText       消息內(nèi)容
     * @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() {
    }
    //唯一實(shí)例入口
    public static SysShareUtil getInstance() {
        if (null == instance) {
            synchronized (SysShareUtil.class) {
                if (null == instance) {
                    instance = new SysShareUtil();
                }
            }
        }
        return instance;
    }

}

設(shè)置應(yīng)用在系統(tǒng)分享時(shí)過(guò)濾匹配的類(lèi)型:

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

補(bǔ)充:
1、如上android:mimeType就是設(shè)置過(guò)濾類(lèi)型,這里的語(yǔ)法是匹配所有的分享類(lèi)型
2、如下是支持的MIME類(lèi)型:

{".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"},  
{"", "*/*"} 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,536評(píng)論 19 139
  • 1 需求分析 當(dāng)前項(xiàng)目中需要將我司的APP加入系統(tǒng)分享中,然后接收外部分享過(guò)來(lái)的數(shù)據(jù)。單純看需求貌似是很簡(jiǎn)單,然后...
    CnPeng閱讀 1,928評(píng)論 0 5
  • 前段時(shí)間,項(xiàng)目要求把系統(tǒng)相冊(cè)的照片分享到應(yīng)用中,后來(lái)看了下iOS8的新特性Extension的分享功能,雖然網(wǎng)上有...
    一個(gè)人一匹馬閱讀 8,776評(píng)論 20 11
  • 3D Touch其實(shí)就是快速進(jìn)入指定app界面的一種方式。下面和大家分享一下簡(jiǎn)單的實(shí)現(xiàn)1、靜態(tài)標(biāo)簽靜態(tài)標(biāo)簽是我們?cè)?..
    航彪彪閱讀 5,242評(píng)論 27 5
  • 因?yàn)樽罱鼊倓傔M(jìn)入新公司,還在熟悉業(yè)務(wù)中,所以帶我的師傅讓我關(guān)注關(guān)注Android的非SDK式的分享,也就是所謂的調(diào)...
    dsx閱讀 2,992評(píng)論 9 9

友情鏈接更多精彩內(nèi)容