无法获取 Environment.DIRECTORY_DOWNLOADS 的外部存储文件目录

问题描述

在应用程序中,我已经实现了下载管理器来下载新的应用程序版本(APK 文件)然后安装它。我最近迁移到范围存储以使用应用程序的私有外部目录。这是代码:

private void setupDownloadRequest() {
    mDownloadRequest = new DownloadManager.Request(Uri.parse(mDownloadUrl));
    mDownloadRequest.setTitle(mFileName);
    mDownloadRequest
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    mDownloadRequest.setMimeType(getMimeType());
    mDownloadRequest.setDestinationInExternalFilesDir(mActivity,Environment.DIRECTORY_DOWNLOADS,mFileName);
}

该过程在 Android

Fatal Exception: java.lang.IllegalStateException: Failed to get external storage files directory
    at android.app.DownloadManager$Request.setDestinationInExternalFilesDir(DownloadManager.java:782)
    ...

查看Android SDK(API 29)中的代码,目录好像没有了:

public Request setDestinationInExternalFilesDir(Context context,String dirType,String subPath) {
    final File file = context.getExternalFilesDir(dirType);
    if (file == null) {
        throw new IllegalStateException("Failed to get external storage files directory");
    ...

你知道如何解决这个问题吗?某些设备上是否缺少 Environment.DIRECTORY_DOWNLOADS 目录,不应该自动创建吗?

在下载之前,我检查是否授予了 Manifest.permission.WRITE_EXTERNAL_STORAGE 权限。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)