解决 Android N 上 报错:android.os.FileUriExposedException
来源:互联网 发布:欧姆龙plc编程实例 编辑:程序博客网 时间:2024/05/03 01:11
前一段时间在调试项目拍照上传的时候,发现在 Android N 上 报错:android.os.FileUriExposedException
测试手机:小米5
系统:MIUI8开发版,android 7.0
查资料后得知报错的原因是:Android N对访问文件权限收回,按照Android N的要求,若要在应用间共享文件,您应发送一项 content://URI,并授予 URI 临时访问权限。
而进行此授权的最简单方式是使用 FileProvider类。
解决办法:
1、在AndroidManifest.xml中添加如下代码
<application
...
<!-- authorities:app的包名.fileProvider grantUriPermissions:必须是true,表示授予 URI 临时访问权限 exported:必须是false resource:中的@xml/file_paths是我们接下来要添加的文件-->
<provider android:name="android.support.v4.content.FileProvider" android:authorities="包名.fileProvider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /></provider>
</application>
2、在res目录下新建一个xml文件夹,并且新建一个file_paths的xml文件(如下图)
file_paths的内容:
<?xml version="1.0" encoding="utf-8"?><paths> <!-- path:需要临时授权访问的路径(.代表所有路径) name:就是你给这个访问路径起个名字 --> <external-path path="Android/data/com.lzy.imagepickerdemo/" name="files_root" /> <external-path path="." name="external_storage_root" /></paths>
3、修改Uri,适配不同的系统
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){ Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file); }else { Uri photoURI = Uri.fromFile(file); }
参考地址:http://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed
0 0
- android-解决 Android N 上 报错:android.os.FileUriExposedException
- android-解决 Android N 上 报错:android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- 解决 Android N 上 报错:android.os.FileUriExposedException
- 解决 Android N 上 报错:android.os.FileUriExposedException
- 解决 Android N 上 报错:android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- Android N共享文件报错:android.os.FileUriExposedException
- (转)Android N安装apk报错:android.os.FileUriExposedException
- 解决android7.0报错:android.os.FileUriExposedException
- 解决 Android N 上 安装Apk时报错:android.os.FileUriExposedException: file:///storage/emulated/0/Download/appN
- Android的SDK版本N以上报错:android.os.FileUriExposedException
- 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- IDEA整合Spring+Springmvc+mybatis+maven详细教程
- 线程的五大状态
- JDK1.8下webservice的wsdl2java 命令出现的bug
- JQuery声明常量
- 博客声明
- 解决 Android N 上 报错:android.os.FileUriExposedException
- 170215
- 七牛云存储____官方js上传(基于pupload插件)
- android activity返回键监听
- windows32位下安装Redis
- gdb调试常用实用命令和core dump文件的生成
- Java 实例说明 获得一个类的类对象有哪些方式?
- Mybatis中的三个标签<where> <set> <trim>
- CentOS中本地yum源的制作