【发布时间】:2019-11-14 06:54:14
【问题描述】:
我正在尝试使用以下代码安装存储在“下载”文件夹下的外部存储中的 .apk 文件。我收到一个错误对话框,其中包含解析包时出现问题的消息。我正在使用从 google play store 下载的生产 .apk 文件以及调试 .apk 文件。对于这两个文件,我在以编程方式安装 .apk 时收到相同的错误屏幕。手动我可以安装相同的 .apk 文件,因此文件中没有问题。我什至将此应用程序设为 Device Owner 应用程序,但它仍然无法正常工作。有没有办法安装.apk 文件?
Uri mUri_ = Uri.parse(filePath);
String packageName = "com.neo.apkinstaller";
context.grantUriPermission(packageName, mUri_, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
Intent appInstallerIntent = null;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
appInstallerIntent = new Intent(Intent.ACTION_VIEW);
appInstallerIntent.setDataAndType(Uri.fromFile(new File(inst_path)), "application/vnd.android.package-archive");
} else {
Uri uri = FileProvider.getUriForFile(context, "com.neo.apkinstaller", new File(inst_path));
appInstallerIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
appInstallerIntent.setData(uri);
appInstallerIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
appInstallerIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(appInstallerIntent);
} catch (Exception e) {
e.printStackTrace();
}
Manifest.xml 文件中的文件提供程序声明:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.neo.apkinstaller"
android:exported="false"
android:multiprocess="true"
android:process="@string/process"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
provider_paths.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="."/>
<files-path
name="files"
path="/"/>
<cache-path
name="cache"
path="/"/>
<external-files-path
name="external_files"
path="."/>
<external-cache-path
name="external_cache"
path="."/>
<files-path
name="tmp"
path="tmp/"/>
<root-path
name="sdcard1"
path="." />
</paths>
【问题讨论】:
标签: android