Android7.0打开本地文件失败 android.os.FileUriExposedException
2017-04-11 11:16
393 查看
错误:Android7.0打开本地文件失败android.os.FileUriExposedException: file:///storage/emulated/0/BE641B.xls exposed beyond app through Intent.getData()。
这是我今天测试遇到的问题,查了一下是Android7.0权限机制改变造成的,下面是我借鉴网络上他人指教的解决方法,现已完美解决。
首先在配置文件中添加如下:你只需要修改划线部位,包名和xml资源的filepaths.xml,不存在的话就创建。
<provider android:authorities="com.navston.shaxi.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 4000 "/> </provider>
这是filepaths.xml内容:name随便命名
<?xml version="1.0" encoding="utf-8"?> <paths> <external-path path="." name="files_path" /> </paths>
这是java文件中获取文件Uri的方法:
Uri uri=null; if (Build.VERSION.SDK_INT >= 24) { uri = FileProvider.getUriForFile(context.getApplicationContext(), "com.navston.shaxi.fileprovider", new File(filePath)); } else { uri = Uri.fromFile(new File(filePath)); }注意:需要intent需要加入下面flags,我试过不加这句不报错,但是无法打开文件。
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);//增加读写权限
这是需要修改的部分,其他未提到的都不需要修改。
相关文章推荐
- android 7.0系统打开本地文件的问题android.os.FileUriExposedException:file:///storage/emulated/0/
- Android 7.0调用系统相机(文件访问crash android.os.FileUriExposedException)
- 解决Android7.0系统 调用系统相机、系统播放器播放视频、切图兼容问题,报异常android.os.FileUriExposedException
- 下载APK后打开APK,在安装时的报错 android.os.FileUriExposedException:exposed beyond app through Intent.getData()
- Android7.0及以上android.os.FileUriExposedException,解析安装包出现异常
- 解决Android7.0相机 android.os.FileUriExposedException
- Android7.0解决 android.os.FileUriExposedException: file:///storage/emulated/0/
- 解决Android7.0系统 调用系统相机、系统播放器播放视频、切图兼容问题,报异常android.os.FileUriExposedException
- Android7.0 android.os.FileUriExposedException
- android 7.0读取文件报异常信息 - android.os.FileUriExposedException
- Android 7.0 android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- 解决android7.0报错:android.os.FileUriExposedException
- 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- Android 7.0调用系统相机报错Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/rpms/14
- android 7.0系统解决拍照的问题android.os.FileUriExposedException:file:///storage/emulated/0/test.txt
- Android的SDK版本N以上报错:android.os.FileUriExposedException
- Android 7.0 系统相机崩溃解决android.os.FileUriExposedException
- 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- 开发中碰到的问题:Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException: file:///storage/emulated/0/And