android10 部分机型(华为)无法正常更新安装app

在android10的环境下 发现部分华为手机不能正常更新安装,检查了一下爆出异常

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file://storage/emulated/0/文件夹名/文件名.apk typ=application/vnd.android.package-archive }
解决方法:
1.mainifest文件下添加:
        
        <provider
                android:name="androidx.core.content.FileProvider"
                android:authorities="包名.fileProvider"
                android:exported="false"
                android:grantUriPermissions="true">
            <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/filepaths" />
        </provider>
2.xml文件:
<resources>
    <paths>
        <external-path path="." name="my_files" />
    </paths>
</resources>
3.安装添加版本判断
  private void installAPK() {
        File apkFile = new File(saveFilePath);
        if (!apkFile.exists()) {
            return;
        }
        Intent i = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { //10.0+以上版本
            Uri apkUri = FileProvider.getUriForFile(context, "包名.fileProvider", apkFile); //与manifest中定义的provider中的authorities="包名.fileprovider"保持一致
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
           i.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            i.setDataAndType(Uri.parse("file://" + apkFile.toString()),
                    "application/vnd.android.package-archive");
        }
        context.startActivity(i);
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值