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 }

解决方法:
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>

xml文件:

<resources>
    <paths>
        <external-path path="." name="my_files" />
    </paths>
</resources>
  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);
    }

你可能感兴趣的:(常见异常,android)