標籤:ext.get app src utf-8 本地 nal files 原因 class
一、 FileUriExposedException 的原因
Android7.0不識別uri以file://開頭,要將其轉換為content://才能識別uri
二、如何解決
1.xml的建立:
file_paths.xml中編寫該Provider對外提供檔案的目錄:檔案放置在res/xml/下。 為了避免和其它app衝突,最好帶上自己app的包名。
檔案內容:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="." name="external_storage_root" />
</paths>
內部的element可以是files-path,cache-path,external-path,external-files-path,external-cache-path,分別對應Context.getFilesDir(),Context.getCacheDir(),Environment.getExternalStorageDirectory(),Context.getExternalFilesDir(),Context.getExternalCacheDir()等幾個方法。後來翻看源碼發現還有一個沒有寫進文檔的,但是也可以使用的element,是root-path,直接對應檔案系統根目錄。不過既然沒有寫進文檔中,其實還是有將來移除的可能的。使用的話需要注意一下風險。
2.Manifests.xml中配置
<manifest>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${APPLICATION_ID}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
...
</application>
</manifest>
3.Uri使用
1)getUriForFile方法轉換:
public static Uri getUriForFile(Context context, File file) {
return FileProvider.getUriForFile(context, GB.getCallBack().getApplicationId(), file);
}
//第二個參數是manifest中定義的`authorities`:因為當時file_paths.xml中賦值為.,故第二個參數是:"com.up366.mobile.fileProvider"
2)intent加Flags
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
註:擷取本地圖片,沒有uri就不用寫
完成。
Android7.0 FileUriExposedException 問題解決