问题描述
我正在尝试以编程方式在设备中安装 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 (将#修改为@)