安装APK会导致解析错误 [英] Installing an APK results in a parse error
本文介绍了安装APK会导致解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在/sdcard/Download
我有此代码:
@Override
public void onClick(View v){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/sdcard/Download/" + "app-debug.apk")),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
我收到此错误:
解析错误
解析程序包时出现问题.
There was a problem parsing the package.
如何在不出现此错误的情况下以编程方式对其进行修改?
How can I modify it programmatically without getting this error?
推荐答案
对于Android N及更高版本,您应使用FileProvider
这样:
For Android N and above you should use FileProvider
like this :
Uri apkUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
在您的AndroidManifest.xml中:
In your AndroidManifest.xml :
<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/provider_paths" />
</provider>
</application>
然后在res/xml
下创建名为provider_paths.xml
的文件:
Then create file named provider_paths.xml
under res/xml
:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
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>
也不要忘记在清单中添加权限:
Don't forget to add permission in manifest also:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
这篇关于安装APK会导致解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文