Android N 7.0 上安装apk问题
2017-11-24 10:40
399 查看
Android N 7.0 上安装apk android.os.FileUriExposedException问题
android.os.FileUriExposedException: file:///storage/emulated/0/trgis/1511427343635.apk exposed beyond app through Intent.getData()
今天做自动更新的时候,自己下载好的apk安装包调用系统的安装服务就报错,很是郁闷,因为之前的代码是好着的,后来查了下资料,原来是Android N 7.0版本之后不支持之前的写法了,好了直接上解决方案。
1.在AndroidManifest.xml application标签中添加如下代码
<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>
注意
authorities:你app的包名.fileProvider
grantUriPermissions:必须是true,表示授予 URI 临时访问权限
exported:必须是false
resource:中的@xml/file_paths是我们接下来要添加的文件
2.在res/xml下新建file_paths.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <paths> <external-path name="files_root" path="" /> </paths> </resources>
注意
path:需要临时授权访问的路径(.代表所有路径)
name:就是你给这个访问路径起个名字
3.适配AndroidN
以前我们直接 Uri.fromFile(apkFile)构建出一个Uri,现在我们使用
FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + “.fileProvider”, apkFile);
BuildConfig.APPLICATION_ID直接是应用的包名
Intent intent = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { /* Android N 写法*/ intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", new File("apk地址")); intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { /* Android N之前的老版本写法*/ intent.setDataAndType(Uri.fromFile(new File("apk地址")), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } startActivity(intent);
相关文章推荐
- Android 7.0 自动安装APK及拍照崩溃问题
- Android 版本更新下载自动安装APK,并解决Android6.0,7.0安装失败的问题
- 开发中碰到的问题:Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException: file:///storage/emulated/0/And
- android 7.0系统安装apk报错问题。
- Android 系统安装 apk 时解压 so 的逻辑问题
- android开发,安装带有特殊字符名称的apk,解析包时出现问题
- Android安装包apk文件在某些版本操作系统上安装解析包出错问题的解决办法
- android 开发 程序中下载安装APK文件 问题汇总 解析程序包时出现问题
- 有关Android APK安装不了的问题—— GC_EXTERNAL_ALLOC freed 与 GC_EXPLICIT freed
- Android 版本更新下载自动安装APK,并解决Android6.0安装失败的问题
- 解决在android平台上apk的覆盖安装so文件不覆盖问题
- Android系统移植与调试之------->安装apk时出现错误Failure [INSTALL_FAILED_DEXOPT]问题解决的方法
- android 6.0以上系统apk自动安装失败问题
- 安装APK文件到Android虚拟机以及db.exe文件和AdbWinApi.dll文件的问题
- android studio 提高编译的速度,安装apk过慢问题解决
- Android 小米安装apk遇到的问题
- Android /data/data/app_file/目录下面安装apk无权限问题
- 关于window中在android虚拟机AVD上安装apk应用问题。
- Android apk 签名后程序无法安装的问题
- Android /data/data/app_file/目录下面安装apk无权限问题