Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

kotlin - downloading on DownloadManager and saving file in setDestinationInExternalFilesDir() is not working on Android 10

Im using DownloadManager to save the mp4 that comes from the server. Im saving the file on storage/Emulated/0/Android/data/<packagename>/files/.Videos. I notice that on Android 9 and android 11 is successfully downloading it. But in Android 10 failed. I tried to enclose it on try{}catch{} method but i can't see anything on logs. I also try to add android:requestLegacyExternalStorage="true" on my Android Manifest.xml but the error still occurs. I also refer on this question which he/she using setDestinationUri() but still i can't find a way. BTW, this is my snippet of the 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)

All response is deeply appreciated :)

question from:https://stackoverflow.com/questions/65923397/downloading-on-downloadmanager-and-saving-file-in-setdestinationinexternalfilesd

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try this, it worked for my pdf download:

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

You can use this by changing the Environment variable, I think.


Edit 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){
            
        }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...