我正在尝试从我的服务器下载update.apk,并使用downloadManager安装应用程序,然后将其保存到应用程序的默认文件夹。它位于Android/data/com.sn.myapp/files/中
所以我试了一下:
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle("App Update");
request.setDescription("Downloading... ");
request.setVisibleInDownloadsUi(true);
String PATH = Objects.requireNonNull(getExternalFilesDir(null)).getAbsolutePath();
request.setDestinationInExternalPublicDir(PATH, "/" + "update" + ".apk");要安装该应用程序:
try {
String PATH = Objects.requireNonNull(getExternalFilesDir(null)).getAbsolutePath();
File file = new File(PATH + "/update.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24) {
Uri downloaded_apk = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
intent.setDataAndType(downloaded_apk, "application/vnd.android.package-archive");
List<ResolveInfo> resInfoList = mContext.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
mContext.grantUriPermission(mContext.getApplicationContext().getPackageName() + ".provider", downloaded_apk, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} else {
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}如果我执行上面的命令,我会得到以下错误:
2021-05-17 13:23:05.488 9432-9432/com.sn.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sn.myapp, PID: 9432
java.lang.IllegalStateException: Not one of standard directories: /storage/emulated/0/Android/data/com.sn.myapp/files
at android.os.Parcel.createExceptionOrNull(Parcel.java:2381)
at android.os.Parcel.createException(Parcel.java:2357)
at android.os.Parcel.readException(Parcel.java:2340)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:755)
at android.content.ContentProviderClient.call(ContentProviderClient.java:613)
at android.content.ContentProviderClient.call(ContentProviderClient.java:601)
at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:576)
at com.sn.myapp.MainActivity$1.onClick(MainActivity.java:71)
at android.view.View.performClick(View.java:7520)
at android.view.View.performClickInternal(View.java:7489)
at android.view.View.access$3600(View.java:826)
at android.view.View$PerformClick.run(View.java:28555)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:233)
at android.app.ActivityThread.main(ActivityThread.java:8010)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)会出什么问题呢?
如果我使用以下代码,而不是上面的代码:
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/myApp/" + "/" + "update" + ".apk");文件已下载,但在访问要安装的文件时出现以下错误。
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: java.lang.IllegalArgumentException: Failed to find configured root that contains /Download/myApp/update.apk
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at com.sn.myapp.MainActivity.installApk(MainActivity.java:196)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at com.sn.myapp.MainActivity.access$400(MainActivity.java:33)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at com.sn.myapp.MainActivity$2.onReceive(MainActivity.java:181)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1566)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at android.os.Handler.handleCallback(Handler.java:938)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at android.os.Looper.loop(Looper.java:233)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:8010)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
2021-05-17 13:47:54.428 9434-9434/com.sn.myapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
2021-05-17 13:47:54.440 9434-9434/com.sn.myapp D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@8e0e98b[MainActivity]
2021-05-17 13:47:54.441 9434-9434/com.sn.myapp D/ViewRootImpl[MainActivity]: windowFocusChanged hasFocus=true inTouchMode=true
2021-05-17 13:48:18.711 9434-17078/com.sn.myapp W/DisplayEventDispatcher: dispatcher 0xb400006f92614f50 ~ ignoring unknown event type 0x6d746f6e
2021-05-17 13:48:18.711 9434-9434/com.sn.myapp W/DisplayEventDispatcher: dispatcher 0xb400006f426157d0 ~ ignoring unknown event type 0x6d746f6e 发布于 2021-05-17 18:33:59
合并
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS)
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);或
request.setDestinationInExternalFilesDir("myfolder")
getExternalFilesDir("myfolder");您将它们混合在一起,并使用完整路径作为参数,该参数将不起作用。
但是..。对于第一个组合,您最好注册一个广播接收器以执行下载完成操作,并使用下载管理器将提供给您的uri来提供该文件。
https://stackoverflow.com/questions/67566050
复制相似问题