如何解决以编程方式安装 apk Android 时解析包时出现问题
我正在尝试以编程方式在设备中安装 apk 文件,但无法安装,因为它说暂存应用程序... 解析包时出现问题。下面显示的是显示的消息
以下是在物理设备中尝试的代码。
private static final String APP_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/";
private void install() {
File file = new File(APP_DIR + "app.apk");
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "application/vnd.android.package-archive";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri downloadedApk = FileProvider.getUriForFile(this,BuildConfig.APPLICATION_ID + ".provider",file);
intent.setDataAndType(downloadedApk,type);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
intent.setDataAndType(Uri.fromFile(file),type);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
} else {
Toast.makeText(this,"ّFile not found!",Toast.LENGTH_SHORT).show();
}
}
在 path.xml
目录中创建了 res/xml
文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="Download" path="Download/"/>
<!-- <external-path name="external" path="." />-->
<external-path
name="external_files"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
已在 AndroidManifest.xml
文件中提供权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<application...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/path" />
</provider>
</application>
还尝试从公共目录保存和安装 apk,但没有帮助
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
还尝试在生成签名的 apk 之前更新应用程序的版本。 请提供以编程方式安装 apk 的解决方案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。