<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/file_paths" /> </provider> ,我来为大家讲解一下关于android 打开apk文件?跟着小编一起来看一看吧!

android 打开apk文件(Android安装APK文件)

android 打开apk文件

在AndroidManifest.xml添加provider

<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/file_paths" /> </provider>

file_paths文件:

<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/APK/res/android"> <external-path name="external_files" path="."/> </paths>

安装APK

File completeApkfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), aApkName); if (!completeApkfile.exists()) { return; } completeApkfile.renameTo(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), aApkName.replace(".md", ".apk"))); File apkfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), aApkName.replace(".md", ".apk")); // 通过Intent安装APK文件 Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri apkUri; // Android 7.0 以上不支持 file://协议 需要通过 FileProvider 访问 sd卡 下面的文件,所以 Uri 需要通过 FileProvider 构造,协议为 content:// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 声明需要的临时权限 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // content:// 协议 apkUri = FileProvider.getUriForFile(mContext.getapplicationContext(), "包名.fileProvider", apkfile); } else { // file:// 协议 apkUri = Uri.fromFile(apkfile); } intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); aContext.startActivity(intent);

,