Android 7.0系统代码调用安装apk时报错FileUriExposedException完美解决
项目更新遇到问题
Android项目开发中经常遇到下载更新的需求,以前调用系统安装器执行安装操作代码如下:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
context.startActivity(intent);
如果Android系统为7.0及以上时则会报异常FileUriExposedException,这是由于安卓官方为了提高私有文件的安全性,面向 Android 7.0 或更高版本的应用私有目录被限制访问 (0700)。此设置可防止私有文件的元数据泄漏,如它们的大小或存在性。传递file:// URI 可能给接收器留下无法访问的路径。因此,尝试传递 file:// URI 会触发 FileUriExposedException。因此需要使用 FileProvider。
Android7.0系统使用FileProvider安装apk安装步骤:
1.manifest.xml文件配置:定义一个FileProvider
<application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="packageName.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
2.添加可用权限的文件目录
在项目res路径下新建名为xml的路径,在xml路径下新建名为file_paths.xml的文件,在file_paths.xml文件中增加如下内容指定分享的路径:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<external-path path="Android/data/packageName/" name="files_root" />
</PreferenceScreen>
3.使用provider直接安装apk:
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", new File(path));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri, context.getContentResolver().getType(contentUri));
//指定打开文件所调用的Activity,若不指定,则会弹出打开方式选择框,
intent.setClassName("com.android.packageinstaller","com.android.packageinstaller.PackageInstallerActivity");
context.startActivity(intent);
完美适配所有系统版本进行apk安装的方式
如上代码虽然可以在Android7.0系统中正常安装apk,但是在低于Android7.0的系统中则不起作用,所以对apk安装调用方法进行封装,完美适配所有系统版本进行apk的安装调用。
/**
* 安装apk
*
* @param context Application对象
* @param path
* apk路径
*/
public static void InstallApk(Context context, String path) {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= 24) {//Android 7.0以上
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", new File(path));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri, context.getContentResolver().getType(contentUri));
//指定打开文件所调用的Activity
intent.setClassName("com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity");
} else {
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
}
context.startActivity(intent);
}
注意事项:
清单文件配置的authorities的值必须与FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", new File(path));方法中第二个参数一致。
Android 7.0系统代码调用安装apk时报错FileUriExposedException完美解决的更多相关文章
- 安装APK时报错:Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
安装APK时报错:Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI] 可以使用adb install -t 解决 对于已经在手机的文件可以使用pm ...
- Android studio 安装apk时报错:INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries
flutter项目 华为手机真机安装报错,解决 办法 app build.gradle android {...}内添加一下代码 splits { abi { enable true reset() ...
- Android安装apk时报错:INSTALL_FAILED_NO_MATCHING_ABIS
问题背景 OS:无关 AS:无关 Genymotion:2.5.2 Virtual Device:Google Nexus 5 - 5.1.0 - API 22 原因分析 CPU架构不符 解决方案 对 ...
- android命令安装apk时报错:INSTALL_FAILED_CPU_ABI_INCOMPATIBLE
点击下载Genymotion-ARM-Translation.zip 将你的虚拟器运行起来,将下载好的zip包用鼠标拖到虚拟机窗口中,出现确认对跨框点OK就行.然后重启你的虚拟机.
- 安装APK报错解决方法【转】
本文转载自:http://blog.csdn.net/zy1235678/article/details/38122827 adb install xxx.apk 报错,安装APK报错:INSTALL ...
- Android 在代码中安装 APK 文件
废话不说,上代码 private void install(String filePath) { Log.i(TAG, "开始执行安装: " + filePath); File a ...
- Android开发之深入理解Android 7.0系统权限更改相关文档
http://www.cnblogs.com/dazhao/p/6547811.html 摘要: Android 6.0之后的版本增加了运行时权限,应用程序在执行每个需要系统权限的功能时,需要添加权限 ...
- 使用拷贝的方式(adb push) 绕过Android系统和adb install直接安装APK
某些情况下定制的Android系统为了限制用户安装应用,例如电视盒子,车载中控等,通过修改代码屏蔽了正常安装应用的方式 本文探讨如何在 adb shell 具有读写data分区目录的权限前提下,通过a ...
- Android 8.0系统的应用图标适配
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 参考资料<一起来学习Android 8.0系统的应用图标适配吧>中已经讲得很清楚了,这里我只是简单总结下.详情的内容请阅 ...
随机推荐
- POJ 1477:Box of Bricks
Box of Bricks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19949 Accepted: 8029 De ...
- iOS中打包.a静态库
1.新建.a静态库工程 需要选择Static Library静态库工程模板新建工程,如下图: 新建静态库工程 实现需要打包的类,如下图: 实现需要打包的类 2.设置需要暴露的头文件 添加Headers ...
- 【Codeforces】665E Beautiful Subarrays
E. Beautiful Subarrays time limit per test: 3 seconds memory limit per test: 512 megabytes input: st ...
- html使用css让文字超出部分用省略号三个点显示的方法案例
html使用css让文字超出部分用省略号三个点显示的方法: 我正确使用的就是下面的代码,li里面是a标记.在IE和google中使用是正常的,火狐不知道,我也不在意,等你来测 li{ display: ...
- [POI2012]FES-Festival
https://www.zybuluo.com/ysner/note/1252538 题面 有一个数列\(\{a\}\).现给定多组限制,限制分成\(2\)类,第一类是\(a_x+1=a_y\),有\ ...
- visual studio使用dos命令在生成项目时复制文件到指定目录
本人使用软件:vs2015 拷贝“项目1”的 bin目录 下, 项目配置的名称(“Release”,“Debug”)目录下,所有内容到“项目2”输出目录(存在直接覆盖): xcopy $(Soluti ...
- Java多线程(九) synchronized 锁对象的改变
public class MyService { private String lock = "123"; public void testMethod() { synchroni ...
- 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)
Description Given a positive integer N, your task is to calculate the sum of the positive integers l ...
- Modbus通讯错误检测方法
标准的Modbus串行网络采用两种错误检测方法.奇偶校验对每个字符都可用,帧检测(LRC和CRC)应用于整个消息.它们都是在消息发送前由主设备产生的,从设备在接收过程中检测每个字符和整个消息帧. 用户 ...
- Python基础:基本数据类型
python基本标准6类数据类型:Number数字, String字符串, List列表,Tuple元组,Set集合,Dictionary字典 不可变数据3个(Number数字,String字符串,T ...