【发布时间】:2018-08-23 10:20:09
【问题描述】:
我正在尝试在 android 8 (Oreo) 设备上以编程方式从我的应用安装应用。
我正在使用以下导致此问题的代码。 我的文件提供者权限定义为
`private static final String AUTHORITY = "com.unicornialab.myapplication.fileprovider";`
我尝试安装的应用程序位于我设备根存储中名为 Alpha1updates 的文件夹中,如第一行所示
`File toInstall = new File(Environment.getExternalStorageDirectory().getPath()
+ File.separator + "Alpha1Updates" + File.separator, "update.apk")`
然后我尝试安装如下所示的应用程序
`if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// Uri contentUri = FileProvider.getUriForFile(this, "com.unicornialab.myapplication", newFile);
Uri apkUri = FileProvider.getUriForFile(context, AUTHORITY, toInstall);
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
} else {
Uri apkUri = Uri.fromFile(toInstall);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}`
由于我使用的是android 8设备,代码在
`if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {}`
在上面的代码中执行。 我已请求获得安装来自未知来源的软件包的权限,例如
`if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!getPackageManager().canRequestPackageInstalls()) {
startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", getPackageName()))), MY_PERMISSIONS_INSTALLFROM_UNKWONSOURCES);
}
}`
我的清单文件中包含了这样的提供程序。
`<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>`
xml 文件夹中的 provider_paths 如下所示
`<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>`
我可能做错了什么解析包时出现问题
任何帮助将不胜感激。
【问题讨论】:
-
嗨。你的问题解决了吗?
标签: android-8.0-oreo android-fileprovider android-8.1-oreo