在 DownloadManager 上下载并在 setDestinationInExternalFilesDir() 中保存文件不适用于 Android 10

问题描述

我使用 DownloadManager 来保存来自服务器的 mp4。我将文件保存在 storage/Emulated/0/Android/data/<packagename>/files/.Videos。我注意到在 Android 9 和 android 11 上成功下载了它。但在 Android 10 中失败了。我试图将它包含在 try{}catch{} 方法中,但我在日志上看不到任何内容。我也尝试在我的 android:requestLegacyExternalStorage="true" 上添加 Android Manifest.xml 但错误仍然发生。我还参考了他/她使用 setDestinationUri()this 问题,但我仍然找不到方法。顺便说一句,这是我的 DownloadRequest 片段:

val path: String =
            context.getExternalFilesDir(null)?.absolutePath + "/" + ".Videos"

val downloadmanager: DownloadManager =
            context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        val request: DownloadManager.Request = DownloadManager.Request(uri)
            .setTitle(videoName)
            .setDescription("Downloading")
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
            .setDestinationInExternalFilesDir(context,".Videos","test1.mp4")
            //.setDestinationUri(Uri.fromFile(File(path,"test.mp4")))
            //.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,videoName)
        val downloadId = downloadmanager.enqueue(request)

非常感谢所有回复:)

解决方法

您可以试试这个,它适用于我的 pdf 下载:

 request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOCUMENTS,id+".pdf");

我认为您可以通过更改 Environment 变量来使用它。


编辑 1.0


 try{
            Uri uri = Uri.parse(downloadPath);

            DownloadManager.Request request = new DownloadManager.Request(uri);
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);  // Tell on which network you want to download file.
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);  // This will show notification on top when downloading the file.
            request.setTitle("Downloading data..."); // Title for notification.
            request.setVisibleInDownloadsUi(true);
            request.addRequestHeader("Authorization","bearer my bearertoken"));
            request.setDestinationInExternalFilesDir(this,id+".pdf");
            ((DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); // This will start downloading
        }catch(Exception e){
            
        }

,

使用 request.setDestinationInExternalFilesDir(this,"filename.pdf"); 仍有问题

这在我的情况下有效:

    val file = File(getPicturesDirPath(this),filename)
    val destUri = Uri.fromFile()
    request.setDestinationUri(destUri)
    
    private fun getPicturesDirPath(activity: Context): String{
            val file: File = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!
            if (!file.exists()) {
                file.parentFile.mkdirs()
                file.mkdir()
            }
    
            return file.absolutePath + "/"
    }
     

确保将 Environment.DIRECTORY_PICTURES 更改为所需目录

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...