解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/

来源:互联网 发布:php防sql注入代码 编辑:程序博客网 时间:2024/06/07 10:39

http://blog.csdn.net/mr_orange_klj/article/details/69660225

1、在AndroidManifest.xml中添加如下代码

复制代码
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    ...    <application        ...        <provider            android:name="android.support.v4.content.FileProvider"            android:authorities="${applicationId}.provider"            android:exported="false"            android:grantUriPermissions="true">            <meta-data                android:name="android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/provider_paths"/>        </provider>    </application></manifest>
复制代码

2、在res目录下新建一个xml文件夹,并且新建一个provider_paths的xml文件

<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">    <external-path name="external_files" path="."/></paths>
注意external-path代表了不同的文件目录,一共有五种,下面是官网的说明:
<files-path name="name" path="path" /> 
Represents files in the files/ subdirectory of your app's internal storage area. This subdirectory is the same as the value returned byContext.getFilesDir().
代表了Context.getFilesDir().
<cache-path name="name" path="path" />
Represents files in the cache subdirectory of your app's internal storage area. The root path of this subdirectory is the same as the value returned by getCacheDir().
<external-path name="name" path="path" />
Represents files in the root of the external storage area. The root path of this subdirectory is the same as the value returned byEnvironment.getExternalStorageDirectory().
<external-files-path name="name" path="path" />
Represents files in the root of your app's external storage area. The root path of this subdirectory is the same as the value returned byContext#getExternalFilesDir(String) Context.getExternalFilesDir(null).
<external-cache-path name="name" path="path" />
Represents files in the root of your app's external cache area. The root path of this subdirectory is the same as the value returned byContext.getExternalCacheDir().

3、修改代码

Uri photoURI = Uri.fromFile(createImageFile());

变成:

Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());

 

1

阅读全文
0 0