Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException解决办法
2017-06-14 15:11
323 查看
这几天项目更新遇到的问题 Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.bjhl.education/ files/xxx.apk exposed beyond app through Intent.getData()的解决办法
1、在AndroidManifest.xml中添加如下代码
<provider android:name="android.support.v4.content.FileProvider" android:authorities="app的包名.fileProvider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/> </provider>
注意:
authorities:app的包名.fileProvider
grantUriPermissions:必须是true,表示授予 URI 临时访问权限
exported:必须是false
resource:中的@xml/file_paths是我们接下来要添加的文件
2、在res目录下新建一个xml文件夹,并且新建一个file_paths的xml文件(如下图)
3、打开file_paths.xml文件添加如下内容
<?xml version="1.0" encoding="utf-8"?> <paths> <external-path path="Android/data/app的包名/" name="files_root"/> <external-path path="." name="external_storage_root"/> </paths>
path:需要临时授权访问的路径(.代表所有路径)
name:就是你给这个访问路径起个名字
4、修改代码适配Android N
Intentintent =newIntent(Intent.ACTION_VIEW); //判断是否是AndroidN以及更高的版本 if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) { Uri contentUri = FileProvider.getUriForFile(context,"com.bjhl.education.fileProvider",file); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(contentUri,"application/vnd.android.package-archive"); }else{ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive"); } context.startActivity(intent);
1、首先我们对Android N及以上做判断;
2、然后添加flags,表明我们要被授予什么样的临时权限
3、以前我们直接Uri.fromFile(apkFile)构建出一个Uri,现在我们使用FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + “.fileProvider”, apkFile);
4、BuildConfig.APPLICATION_ID直接是应用的包名
相关文章推荐
- Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException(已修复)
- 开发中碰到的问题:Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException: file:///storage/emulated/0/And
- android 7.0 apk版本更新,下载之后自动安装
- Android 版本更新下载自动安装APK,并解决Android6.0,7.0安装失败的问题
- android版本更新适配7.0,解决7.0手机无法更新安装apk
- Android获取SD卡上的APK文件,并且判断是否安装过、是否可以更新到新版本的工具类
- Android 代码自动升级新版本更新apk时出现“已安装了存在签名冲突的同名数据包”
- 转载 :Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- [Android开发] android7.0+安装apk文件的姿势
- Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- android更新安装apk
- Android 更新下载apk 安装apk 打开apk
- Android获取SD卡上的APK文件,并且判断是否安装过、是否可以更新到新版本的工具类
- Android 版本更新下载自动安装APK,并解决Android6.0安装失败的问题
- Android APK 安装/更新问题
- Android客户端apk自动检测更新自动下载自动安装的实现方法
- 关于 android apk安装 更新 卸载的几个问题
- <为知更新>Android下一个apk安装多个程序入口图标
- Android获取SD卡上的APK文件,并且判断是否安装过、是否可以更新到新版本的工具类
- Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)