当前位置:网站首页>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();
边栏推荐
- 如何利用數倉創建時序錶
- Another short video app with high imitation and eye opening
- Three Solution to the problem of inaccuracy in radiographic testing under the condition of non full screen canvas of JS
- 【FreeRTOS】07 二值信号量、计数信号量
- 【面试经验包】面试被吊打经验总结(一)
- GBASE观察:扩展分析型数据库
- Wechat applet picture verification code display
- Comment utiliser l'entrepôt de données pour créer une table de synchronisation
- 点乘和叉乘
- Synthetic big watermelon games wechat applet source code / wechat game applet source code
猜你喜欢

Web site SSL certificate

Visual explanation of clockwise inner curve in Green's formula hole digging method

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

List<? Extensions T > and list <? Super T > difference

Docker deploy redis

Docker redis cluster configuration

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

Flux in three dimensional vector field

Use of reverse tools IDA and GDB

APP性能优化之启动流程分析
随机推荐
Generative countermeasure networks (Gans) and variants
Smart doc + Torna compatible version
Tiktok practice ~ password retrieval
如何利用数仓创建时序表
AI技术在医学领域有什么用?
Different objects use the same material and have different performances
合成大西瓜小游戏微信小程序源码/微信游戏小程序源码
工作中一些常用的工具函數
EasyCVR程序以服务启动异常,进程启动却正常,是什么原因?
Cloud native architecture (05) - Application Architecture Evolution
Visual explanation of clockwise inner curve in Green's formula hole digging method
What is the difference between overload and override?
三维向量场中的通量
Quantitative investment model -- research interpretation of high frequency trading market making model (Avellaneda & Stoikov's) & code resources
[technical grass planting] the tail of the "double 11" event. Let's talk about how much discount the message push service package is!
[technical grass planting] use the shared image function to realize the offline switching from CVM to LH
Idea automatically generates unit tests, doubling efficiency!
First week of summer vacation
Wechat applet picture verification code display
2.摄像机标定