【问题标题】:Android DownloadManager illegalstateexception unable to create directoryAndroid DownloadManager 非法状态异常无法创建目录
【发布时间】:2023-04-06 00:37:02
【问题描述】:

我正在制作使用 DownloadManager 的 android 应用程序。 我想将文件下载到我制作的文件夹中。 但是这个来源不起作用。 并发生 IllegalstateException。 我能做什么??

urlToDownload = Uri.parse(URL);
List<String> pathSegments = urlToDownload.getPathSegments();
request = new DownloadManager.Request(urlToDownload);
request.setTitle(Titlename);
request.setDescription("MCPE STORE");
request.setDestinationInExternalPublicDir(
                   Environment.getExternalStorageDirectory().getAbsolutePath() + 
                   "/MCPE STORE", pathSegments.get(pathSegments.size()-1));

Environment.getExternalStoragePublicDirectory(
                   Environment.getExternalStorageDirectory().getAbsolutePath() + 
                   "/MCPE STORE").mkdir();
latestId = downloadManager.enqueue(request);

【问题讨论】:

    标签: android illegalstateexception create-directory


    【解决方案1】:

    我能做什么?

    如果你阅读the documentation for setDestinationInExternalPublicDir(),你会看到第一个参数是“目录类型to pass to getExternalStoragePublicDirectory(String)”。这需要是Environment 类上定义的常量之一,例如Environment.DIRECTORY_DOWNLOADS。您正在传递其他不受支持的内容。

    【讨论】:

    • 如果我想在我的应用程序的特殊文件夹中下载文件,我应该怎么做?只需将文件下载到环境类定义的常量文件夹中,然后剪切/粘贴文件?有什么想法吗?
    • @AliBehzadianNejad 如果我没听错的话,我是这样做的:request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, File.separator + folderName + File.separator + filename);
    【解决方案2】:

    确保你有

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    在你的manifest.xml

    此外,如果您使用的是模拟器,请确保使用 SD 卡存储创建它。它不是默认创建的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 2013-01-13
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多