当前位置:网站首页>Android 7,2021最新Android面试笔试题目分享
Android 7,2021最新Android面试笔试题目分享
2022-06-23 22:20:00 【普通网友】
android.os.FileUriExposedException: file:///storage/emulated/0/Download/xxxAppName.apk exposed beyond app through Intent.getData()
手机端调用的代码如下:
Intent intentUpdate = new Intent(“android.intent.action.VIEW”);
intentUpdate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri apkUri = Uri.fromFile(new File(upgradeMsg.apkpath));
intentUpdate.setDataAndType(apkUri, “application/vnd.android.package-archive”);
startActivity(intentUpdate);
报这个错误主要是应为google在6.0以后的版本做了权限限制, 应用程序 不能直接通过**file://Uri向另一个共享资源,需要通过content://Uris **来共享资源,以便平台可以扩展接收应用程序的临时权限以访问资源。但是N之前的版本还是可以通过 file:// 的方式去共享资源的。
主要原因总结如下:
- 假如共享的文件是私有的,接收file://Uri 的App无法访问文件
- 在Android6.0之后引入运行时权限,如果file://Uri 的app没有申请 Manifest.permission.READ_EXTERNAL_STORAGE权限,在读取文件时 会引发崩溃
解决这种错误的方法google也给出了方案,用FileProvider,它为文件 创建 content://Uri 而不是 file://Uri 来做文件的安全共享。
FileProvider主要步骤:
- 定义FileProvider
- 指定可用文件
- 检索文件内容的URI
- 授权URI临时权限
- 向另一个应用程序提供内容URI
在AndroidManifest.xml中添加如下代码
注意: authorities:app的包名.fileProvider grantUriPermissions:必须是true,表示授予 URI 临时访问权限 exported:必须是false resource:中的@xml/file_paths是我们接下来要添加的文件
在res目录下新建一个xml文件夹,并且新建一个file_paths的xml文件
path:需要临时授权访问的路径(.代表所有路径) name:就是你给这个访问路径起个名字
现在就差修改安装时候的代码了,代码如下:
private void installApk() { //安装程序
Intent intentUpdate = new Intent(“android.intent.action.VIEW”);
intentUpdate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //对Android N及以上的版本做判断
Uri apkUriN = FileProvider.getUriForFile(MainActivity2.this,
MainActivity2.this.getApplicationContext().getPackageName() + “.FileProvider”, new File(upgradeMsg.apkpath));
intentUpdate.addCategory(“android.intent.category.DEFAULT”);
intentUpdate.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //天假Flag 表示我们需要什么权限
intentUpdate.setDataAndType(apkUriN, “application/vnd.android.package-archive”);
} else {
Uri apkUri = Uri.fromFile(new File(upgradeMsg.apkpath));
intentUpdate.setDataAndType(apkUri, “application/vnd.android.package-archive”);
}
startActivity(intentUpdate);
}
代码写好了,是不是以为就可以了? 当你开开心心的在 6.0 或者7.0 ~ 8.0之间的收做更新安装时候,无丝毫毛病,但是当你在8.0及以上版本做安装的时候,点击更新 会一闪而过,或者解析包出错。为什么呢?
因为在Aandroid 8.0 时候Google又做了一些限制操作
Android 8.0 Oreo 中,Google 移除掉了容易被滥用的“允许位置来源”应用的开关,在安装 Play Store 之外的第三方来源的 Android 应用的时候,竟然没有了“允许未知来源”的检查框,如果你还是想要安装某个被自己所信任的开发者的 app,则需要在每一次都手动授予“安装未知应用”的许可。
我们得适配8.0 及以上的版本机型。
首先需要在AandroidManifest.xml 中增加权限
其次再点击更新时候需要判断手机版本信息:
private static final
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
int INSTALL_PACKAGES_REQUESTCODE = 10011;
private static final int GET_UNKNOWN_APP_SOURCES = 10012;
private void checkAndroidO() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //系统 Android O及以上版本
//是否需要处理未知应用来源权限。 true为用户信任安装包安装 false 则需要获取授权
boolean canRequestPackageInstalls = getPackageManager().canRequestPackageInstalls();
if (canRequestPackageInstalls) {
installApk();
边栏推荐
猜你喜欢

Application of acrel-3000web power management system in Duba Expressway

AI技术在医学领域有什么用?
![Restore IP address [standard backtracking + standard pruning]](/img/e6/5f9d2a5af973b6c7051ed434a4b93d.png)
Restore IP address [standard backtracking + standard pruning]
![[proteus simulation] example of T6963C driving pg12864 (with Chinese and English display)](/img/e7/d36750c729b76f18418589356706f1.png)
[proteus simulation] example of T6963C driving pg12864 (with Chinese and English display)

Multi store drug inventory system source code large chain drugstore management system source code

Cloud native architecture (05) - Application Architecture Evolution

How to ensure reliable power supply of Expressway

Confused test / development programmers, different people have different stories and different puzzles

电子元器件行业B2B交易管理系统:提升数据化驱动能力,促进企业销售业绩增长

1. < tag dynamic programming and path combination problem > lt.62 Different paths + lt.63 Different paths II
随机推荐
How to achieve energy-saving and reasonable lighting control in order to achieve the "double carbon" goal
合成大西瓜小游戏微信小程序源码/微信游戏小程序源码
如何利用數倉創建時序錶
. Net
Three Solution to the problem of inaccuracy in radiographic testing under the condition of non full screen canvas of JS
[bug] the element in C iqueryable cannot change its value
微信小程序中three.js的canvas非全屏情况下射线检测不准确问题解决方案
String s = new string ("XYZ") how many string objects are created?
[technical grass planting] the tail of the "double 11" event. Let's talk about how much discount the message push service package is!
== 和 equals 的区别是什么?
NLP-D58-nlp比赛D27&刷题D14&读论文&mathtype
多门店药品进销存系统源码 大型连锁药店管理系统源码
Recommend 4 flutter heavy open source projects
医疗是什么?AI医疗概念解析AI
Facebook open source shimmer effect
工作中一些常用的工具函數
All 32 keyword classifications of C language C89 (C90)
Keywords such as extern and struct
MySQL architecture (basic)
Cross domain issues of the new version of Google browser