解决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

Android7.0解决 android.os.FileUriExposedException: file:///storage/emulated/0/相关推荐

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

    解决android N文件访问crash android.os.FileUriExposedException file:///storage/emulated/0/xxx 原因: Android N ...

  2. Android打开相机,报出 android.os.FileUriExposedException: file:///storage/emulated/0/test/img.jpg exposed

    执行以下代码后,打开相机 //调用相机自带的照相功能 指定图片目录 if (Environment.getExternalStorageState().endsWith(Environment.MED ...

  3. android os FileUriExposedException file storage emulated 0 test tx

    错误出现原因: 在Android版本7.0以上,应用之间文件访问权限修改. 解决办法: 我们需要使用FileProvider允许其他应用访问本应用创建的文件或文件夹,首先需要创建继承android.s ...

  4. android.os.FileUriExposedException: file:///storage/emulated/0/pictures/1564739776566.jpg exposed be

    问题: 原因:Android N(对应sdk24)(版本7.0)及以上对访问文件权限收回,按照Android N的要求,若要在应用间共享文件,您应发送一项 content://URI,并授予 URI ...

  5. android.os.FileUriExposedException: file:///storage/emulated/0/myxmpp/154094

    在项目的MainActivity的开始添加下面这段代码 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {StrictMode.VmPol ...

  6. android.os.FileUriExposedException: file:///storage/emulated/0/myimage/15115

    复制的https://blog.csdn.net/qq_33210042/article/details/78626701 管用!已收藏 //取消严格模式 FileProvider if (Build ...

  7. FileUriExposedException: file:///storage/emulated/0/Android/data/com.skyrin.bingo/cache/app/app.apk

    安卓在app更新的时候file parseuri 报错FileuriExposedException:.................exposed beyond app through Inten ...

  8. FileUriExposedException: file:///storage/emulated/0/001/1612321465868.mp4 exposed beyond app through

    我在调用系统相机的时候报错 private void openCamera() {Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE) ...

  9. 安卓开发拍照时闪退遇到问题: file:///storage/emulated/0/0.jpg exposed beyond app through ClipData.Item.getUri()

    问题描述: 打开摄像头拍照时程序闪退,错误提示如下: android.os.FileUriExposedException: file:///storage/emulated/0/RadioDirec ...

最新文章

  1. 机器学习流程,以及实践应用
  2. elasticsearch 分页_[Springboot实战系列]整合ElasticSearch实现数据模糊搜索
  3. mysql 编译cpu数量_合适MySQL的Xeon 55XX系列CPU编译参数
  4. 【译】Thoughts and Goals on Qtum's x86 VM
  5. unsw计算机科学的挂科率,澳洲新南威尔士大学考试挂科率达41%
  6. Android----paint触摸轨迹监听
  7. kolla-ansible-----常用命令
  8. App for Android (1) —— 环境搭建
  9. 什么是序列化和反序列化
  10. iOS - 发送邮件
  11. oracle函数 NLS_INITCAP(x[,y])
  12. Angular.js 简单实现数字变汉字
  13. Java IO流复制文件拒绝访问的原因及解决方法
  14. 64位Python读取周立功USBCAN-I信息
  15. autojs 复制到粘贴板_JS复制到剪贴板示例代码
  16. 微信全球MBA创新大赛Roadshow首站登陆斯坦福
  17. 圣诞礼物送什么 荣耀畅玩5为你排忧解难
  18. vue使用百度地图获取位置信息
  19. 计计算机类学科代码是多少,高校专业代码表
  20. 12、字符(串)输入、输出

热门文章

  1. 绘图计算机配置清单,主攻专业设计制图 5000元以下i5-7500独显电脑配置清单推荐...
  2. 全景地图是什么?怎么用有什么作用!
  3. 白皮书的参考文献格式怎么写?
  4. 低功耗、高性能智能开发主板MED3568
  5. DPABI(用于脑成像的数据处理和分析的工具箱)的下载和安装步骤
  6. 可自建中继服务器的远程桌面软件: RustDesk
  7. 零经验转型AI产品经理 —— 一些对跨行、跨职能转型者的建议
  8. POE万兆上联网管POE交换机的特点
  9. 1 深度学习为甚么如此火热发展如此迅速
  10. 2020 对自己好一点之新 Mac Book Pro 环境的安装