android隐式intent安装apk,androidN通过intent安装apk需要使用FileProvider

在Android 7.0及更高版本中,使用隐式intent直接安装apk会导致`FileUriExposedException`。解决方法是引入`FileProvider`,在AndroidManifest.xml中声明并指定文件路径,然后为Uri授予临时读写权限。通过修改安装apk的方法,使用`FileProvider.getUriForFile()`在Android N及以上版本中安装apk。
摘要由CSDN通过智能技术生成

有问题的安装:

之前可以直接这样安装apk:

/** *@param file *@return *@Description 安装apk */

public void installApk(File file) {

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.fromFile(file),

"application/vnd.android.package-archive");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(intent);

}

但是在androud N上会报错,

android.os.FileUriExposedException

Android 7.0强制启用了被称作 StrictMode的策略,带来的影响就是你的App对外无法暴露file://类型的URI了。

如果你使用Intent携带这样的URI去打开外部App(比如:打开系统相机拍照),那么会抛出FileUriExposedException异常。

进入正题:开始解决异常

1、定义FileProvider

在Androidmanifest.xml文件中声明:

...

...

android:name="android.support.v4.content.FileProvider"

android:au

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值