1. 程式人生 > >Android呼叫WPS第三方App開啟PDF文件,一直停留在首頁,提示正在載入文件型別

Android呼叫WPS第三方App開啟PDF文件,一直停留在首頁,提示正在載入文件型別

Android 7.0 以後對Uri的訪問進行了限制,需要在manifest專案清單檔案裡面新增 provider,具體怎麼寫這個就不說了。

解決此問題只需新增以下程式碼即可:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

意思就是在程式中動態的grant URI許可權給Action接收者。

    /**
     * 獲取開啟文件意圖
     *
     * @param file
     * @return
     */
    public static Intent getFileIntent(Activity activity, File file) {
        Uri uri = Uri.fromFile(file);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //通過FileProvider建立一個content型別的Uri
            uri = FileProvider.getUriForFile(activity, "com.ss.fileprovider", file);
        }
        String type = getMIMEType(file);
        //優先使用WPS開啟
        //檢測是否安裝了wps軟體,沒有安裝選擇預設開啟, 動態的grant URI許可權
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(uri, type);
        if (SystemUtil.isInstall(activity, "cn.wps.moffice_eng")) {
            intent.setClassName("cn.wps.moffice_eng",
                    "cn.wps.moffice.documentmanager.PreStartActivity2");
            //intent.setData(uri);
        } else {
            intent.addCategory("android.intent.category.DEFAULT");
        }
        return intent;
    }

參考程式碼:

    //建立一個檔案型別與檔案字尾名的匹配表
    private static final String[][] MATCH_ARRAY={
            //{字尾名,    檔案型別}
            {".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/plain"},
            {".z",        "application/x-compress"},
            {".zip",    "application/zip"},
            {"",        "*/*"}
    };

    /**
     * 根據路徑開啟檔案
     * @param context 上下文
     * @param path 檔案路徑
     */
    private static void openFileByPath(Context context,String path) {
        if(context==null||path==null)
            return;
        Intent intent = new Intent();
        //設定intent的Action屬性
        intent.setAction(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        //檔案的型別
        String type = "";
        for(int i =0;i<MATCH_ARRAY.length;i++){
            //判斷檔案的格式
            if(path.toString().contains(MATCH_ARRAY[i][0].toString())){
                type = MATCH_ARRAY[i][1];
                break;
            }
        }
        try {
            File out = new File(path);
            Uri fileURI;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                fileURI = FileProvider.getUriForFile(context,
                        "com.lonelypluto.test.provider",
                        out);
            }else{
                fileURI = Uri.fromFile(out);
            }
            //設定intent的data和Type屬性
            intent.setDataAndType(fileURI, type);
            //跳轉
            context.startActivity(intent);
        } catch (Exception e) { //當系統沒有攜帶檔案開啟軟體,提示
            Toast.makeText(this,"無法開啟該格式檔案",Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

完!!!