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

标签 android kotlin android-10.0 download-manager

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

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, Environment.DIRECTORY_DOCUMENTS, id+".pdf");
            ((DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); // This will start downloading
        }catch(Exception e){
            
        }

关于android - 在 DownloadManager 上下载并将文件保存在 setDestinationInExternalFilesDir() 中不适用于 Android 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65923397/

相关文章:

java - 如何在接口(interface)实例中调用super方法?

java - 仅加密图像文件的内容而不是整个文件

kotlin - 如何获取 @JsonProperty 名称列表?

android - MediaStore.MediaColumns#DATE_TAKEN 是否仅适用于 API 级别 29?

android - 安装不再适用于 Android Q 的自签名证书

java - Android 10 中没有适用于 Android 开发人员的 IMEI

android - 文件 logback.xml 中不支持的类型 'property'

java - 如何解析android sdk 28导入语句中的 'Cannot resolve symbol'

spring-mvc - Thymeleaf 提交表单导致 KotlinNullPointerException

android - 将对象发送到另一个 fragment 后进行更改