解決Android7.0嚴苛模式問題

之前搞二次開發,發現版本太老,遇到各種問題,關於7.0的嚴苛模式,這裏做個筆記下次直接CV。

推薦配置

配置provider

    <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="apk包名.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

file_paths

<?xml version="1.0" encoding="utf-8"?>
<resource xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path name="root" path=""/>
    <files-path name="files" path=""/>
    <cache-path name="cache" path=""/>
    <external-path name="external" path="."/>
    <external-files-path name="external_files" path=""/>
    <external-cache-path name="external_cache" path=""/>
</resource>

存文件

    //插入圖片
    private void insertImage() {
        mImageFile = new File(Environment.getExternalStorageDirectory(), getTime() + ".png");
//        mImageUri = Uri.fromFile(mImageFile);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        mImageUri = FileProvider.getUriForFile(this, "apk包名.fileprovider", mImageFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//這裏加入flag
        startActivityForResult(intent, TAKE_IMAGE);
    }

    //插入視頻
    private void insertVideo() {
        mVideoFile = new File(Environment.getExternalStorageDirectory(), getTime() + ".mp4");
//        mVideoUri = Uri.fromFile(mVideoFile);
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        mVideoUri = FileProvider.getUriForFile(this, "apk包名.fileprovider", mVideoFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mVideoUri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//這裏加入flag
        startActivityForResult(intent, TAKE_VIDEO);
    }

取文件

public void openFile(Context context, String path) {
        File file = new File(path);
        if (null != file && !file.exists()) {
            Toast.makeText(context, "打開失敗,請重試!", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //設置intent的Action屬性
            intent.setAction(Intent.ACTION_VIEW);
            //獲取文件file的MIME類型
            String type = getMIMEType(file);
            //設置intent的data和Type屬性。
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//這裏加入flag
            intent.setDataAndType(FileProvider.getUriForFile(context, "apk包名.fileprovider", file), type);
            //7.0以下使用
//            intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
            //跳轉
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(context, "打開失敗,請重試!", Toast.LENGTH_SHORT).show();
        }
    }

    private String getMIMEType(File file) {

        String type = "*/*";
        String fName = file.getName();
        //獲取後綴名前的分隔符"."在fName中的位置。
        int dotIndex = fName.lastIndexOf(".");
        if (dotIndex < 0) {
            return type;
        }
        /* 獲取文件的後綴名*/
        String end = fName.substring(dotIndex, fName.length()).toLowerCase();
        if (end == "") return type;
        //在MIME和文件類型的匹配表中找到對應的MIME類型。
        for (int i = 0; i < MIME_MapTable.length; i++) { //MIME_MapTable??在這裏你一定有疑問,這個MIME_MapTable是什麼?
            if (end.equals(MIME_MapTable[i][0]))
                type = MIME_MapTable[i][1];
        }
        return type;
    }

    private final String[][] MIME_MapTable = {
            //{後綴名,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"},
            {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
            {".xls", "application/vnd.ms-excel"},
            {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
            {".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"},
            {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
            {".prop", "text/plain"},
            {".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/x-zip-compressed"},
            {"", "*/*"}
    };

快速解決

直接關閉嚴苛模式

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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章