Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android java.lang.IllegalStateException:非标准目录使用DownloadManager时出错_Android_Android Download Manager - Fatal编程技术网

Android java.lang.IllegalStateException:非标准目录使用DownloadManager时出错

Android java.lang.IllegalStateException:非标准目录使用DownloadManager时出错,android,android-download-manager,Android,Android Download Manager,我正在尝试从服务器下载update.apk,并使用downloadManager安装应用程序,然后将其保存到应用程序的默认文件夹中。它位于Android/data/com.sn.myapp/files中/ 所以我试了一下: DownloadManager.Request request = new DownloadManager.Request(Download_Uri); request.setAllowedNetworkTypes(DownloadManage

我正在尝试从服务器下载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();
    }
有什么不对劲吗

如果我使用以下代码,则不使用上述代码:

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 
结合

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS)
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

您将它们混合在一起,并使用完整的路径作为参数,但该参数不会消失


但是。。。对于第一个组合,您最好注册一个广播接收器,以便完成操作下载,并使用下载管理器将提供给您的uri来提供文件。

第二个组合可以。但提供者的设置需要调整。
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS)
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
request.setDestinationInExternalFilesDir("myfolder")
getExternalFilesDir("myfolder");