【问题标题】:How to Download File Using DownloadManager in API 28 or Android Pie?如何在 API 28 或 Android Pie 中使用 DownloadManager 下载文件?
【发布时间】:2020-06-03 04:33:25
【问题描述】:

写入包 java.lang.IllegalStateException 异常:不是标准目录之一:/storage/emulated/0/Android/data.......

 DownloadManager dm = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
                    Uri uri = Uri.parse(s);
                    DownloadManager.Request req = new DownloadManager.Request(uri);
                    req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    String filename = VideoTitle;
                    filename += ".mp4";

                    req.setDestinationInExternalPublicDir(context.getResources().getString(R.string.video_dir_path), filename);
                    req.setDestinationInExternalFilesDir(context,dir_path,filename);
                    StyleableToast.makeText(context,context.getResources().getString(R.string.download_started), Toast.LENGTH_SHORT, R.style.mytoast).show();

                    Long ref = dm.enqueue(req);

如何解决这个错误??

【问题讨论】:

    标签: download-manager


    【解决方案1】:

    我相信问题出在你提到的那一行。您正在设置的连接将文件的目录更改为非标准

    例如:

    request.setDestinationInExternalPublicDir(getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/NewFile","abc.jpg")
    

    错误表明了这一点。看到这行告诉你它试图保存到哪个目录

    类似的东西

    /storage/emulated/0/Android/data/com.a.b.downloadmanager/files/Pictures/NewFile
    

    所以当你这样做时,它会尝试另存为

    /storage/emulated/0/Android/data/com.a.b.downloadmanager/files/Pictures/NewFile/abc.jpg
    

    解决方案是将文件保存到标准目录。为此,您只需要删除串联即可。

    例如:

    request.setDestinationInExternalPublicDir(getExternalFilesDir(Environment.DIRECTORY_PICTURES),"abc.jpg")
    

    然后它会尝试保存到标准目录

    /storage/emulated/0/Android/data/com.a.b.downloadmanager/files/Pictures/abc.jpg
    

    并确保你有

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

    确保你有

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

    在您的 ma​​nifest.xml

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

    【讨论】:

    • 我试过了,但 DownloadManager 不适用于 Android Vivo 1812 版本 8.1.0
    • 检查这个link,例子是here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    相关资源
    最近更新 更多