解决 Android N 上 报错:android.os.Fi
2016-11-28 本文已影响2303人
Mr_不靠谱_先森
问题描述:
解决AndroidN 上 安装Apk时报错:android.os.FileUriExposedException: file:///storage/emulated/0/Download/appName-2.3.0.apk exposed beyond app through Intent.getData()
解决方法:
1、在AndroidManifest.xml中添加如下代码

2、在res目录下新建一个xml文件夹,并且新建一个file_paths的xml文件(如下图)

备注:alt+enter键 自动创建不出错
3、打开file_paths.xml文件添加如下内容

4、修改代码适配Android N

1、首先我们对Android N及以上做判断;
2、然后添加flags,表明我们要被授予什么样的临时权限
3、以前我们直接Uri.fromFile(apkFile)构建出一个Uri,现在我们使用FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);
4、BuildConfig.APPLICATION_ID直接是应用的包名
参考地址:http://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed