我已将 .apk
文件上传到保管箱。我想在用户单击即时应用程序中的安装按钮时下载并启动应用程序。但调用startActivity()
后出现以下异常:
Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{8593d11 2399:com.google.android.packageinstaller/u0a26} (pid=2399, uid=10026) that is not exported from uid 10298
Intent intent = new Intent(Intent.ACTION_VIEW);
if (intent.resolveActivity(getPackageManager()) == null) {
Log.e("No Packate Manager", intent.getPackage() + "");
return;
}
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //BuildConfig.APPLICATION_ID
// File file = new File(File folder, "app-debug.apk");
//uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".fileprovider", file);
uri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".fileprovider", new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "app-debug.apk"));
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
} else {
// uri = Uri.fromFile(new File("/mnt/sdcard/Download/app-debug.apk"));
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File outputFile = new File(folder, "app-debug.apk");
uri = Uri.fromFile(outputFile);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// install.setDataAndType(myuri, manager.getMimeTypeForDownloadedFile(downloadId));
startActivity(intent);
最佳答案
无法从免安装应用启动 Play 商店外部托管的 APK 文件的安装(也不建议这样做)。
您收到的错误是因为即时应用程序无法读取(或写入)外部磁盘存储。有关即时应用程序中受限功能的更多信息,请参阅docs 。尽管即时应用可以读取/写入自己的内部存储,但它们无法将该数据公开或共享给其他应用。
建议的方法是将常规 APK 发布到 Google Play,然后使用提供的 showInstallPrompt()
启动安装的帮助程序方法。这会将用户引导至 Google Play 来安装您的应用。
您还应该查看我们的 UX guidelines用于呈现这样的安装按钮(称为“显式安装提示”)。
关于java - 从 Instant App 下载并安装 apk(无需在 Playstore 中发布),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44382549/