Android7.0解決 android.os.FileUriExposedException: file:///storage/emulated/0/

解決Android N文件訪問crash android.os.FileUriExposedException file:///storage/emulated/0/xxx

原因:

Android N對訪問文件權限收回,按照Android N的要求,若要在應用間共享文件,您應發送一項 content://URI,並授予 URI 臨時訪問權限。
而進行此授權的最簡單方式是使用 FileProvider類。
1.在mainfest中加入FileProvider註冊

<application>
     <provider
android:authorities="你的應用名.fileprovider"              android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
         <meta-data
           android:name="android.support.FILE_PROVIDER_PATHS"
               android:resource="@xml/filepaths"/>
    </provider>
</application>

2.配置filepaths文件

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="yang/" name="files_path" />
</paths>

其中:
files-path代表的根目錄: Context.getFilesDir()
external-path代表的根目錄: Environment.getExternalStorageDirectory()
cache-path代表的根目錄: getCacheDir()

<external-path path="honjane/" name="files_path" />

path 代表要共享的目錄
name 只是一個標示,隨便取吧 自己看的懂就ok

for example:通過provider獲取到的uri鏈接

content://com.ys.providerdemo.fileprovider/files_path/files/b7d4b092822.pdf

name對應到鏈接中的files_path

path對應到鏈接中的 files ,當然files是在ys/目錄下
3.訪問文件

/**
     * 打開文件
     * 當手機中沒有一個app可以打開file時會拋ActivityNotFoundException
     * @param context     activity
     * @param file        File
     * @param contentType 文件類型如:文本(text/html)     
     */
    public static void startActionFile(Context context, File file, String contentType) throws ActivityNotFoundException {
        if (context == null) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setDataAndType(getUriForFile(context, file), contentType);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 打開相機
     *
     * @param activity    Activity
     * @param file        File
     * @param requestCode result requestCode
     */
    public static void startActionCapture(Activity activity, File file, int requestCode) {
        if (activity == null) {
            return;
        }
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, getUriForFile(activity, file));
        activity.startActivityForResult(intent, requestCode);
    }

    private static Uri getUriForFile(Context context, File file) {
        if (context == null || file == null) {
            throw new NullPointerException();
        }
        Uri uri;
        if (Build.VERSION.SDK_INT >= 24) {
            uri = FileProvider.getUriForFile(context.getApplicationContext(), "你的應用名.fileprovider", file);
        } else {
            uri = Uri.fromFile(file);
        }
        return uri;
    }

同樣訪問相機相冊都通過FileProvider.getUriForFile申請臨時共享空間
已寫成工具類上傳到github,需要直接下載
使用方法簡單,一行代碼搞定
打開文件:
打開文件:

 try {
      FileUtils.startActionFile(this,path,mContentType);
    }catch (ActivityNotFoundException e){

 }

調用相機:

 FileUtils.startActionCapture(this, file, requestCode);

修復bug:

Java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/xxx/xxx/file/12b31d2cab6ed.pdf

external與storage/emulated/0/對應,乍一看貌似沒什麼問題,path設置的是external的根路徑,對應Environment.getExternalStorageDirectory(),

然而這個方法所獲取的只是內置SD卡的路徑,所以當選擇的相冊中的圖片是外置SD卡的時候,就查找不到圖片地址了,因此便拋出了failed to find configured root that contains的錯誤。

通過分析FileProvider源碼發現,在xml解析到對應的標籤後,會執行 buildPath() 方法來將根標籤(files-path,cache-path,external-path等)對應的路徑作爲文件根路徑,

在buildPath(),會根據一些常量判斷是構建哪個目錄下的path,除了上面介紹的幾種path外還有個TAG_ROOT_PATH = “root-path” ,只有當不是root-path時纔會去構建其他path,

官方也沒介紹這個root-path,測試了一下發現對應的是DEVICE_ROOT指向的整個存儲的根路徑,這個bug就修復了

修改filepaths文件:

<paths>
      <root-path name="honjane" path="" />
</paths>

代碼下載:https://github.com/yangsongsong/fileProviderDemo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章