downloading on DownloadManager and saving file in setDestinationInExternalFilesDir() is not working on Android 10
Asked Answered
D

3

7

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 :)

Dearing answered 27/1, 2021 at 16:35 Comment(13)
sorry about that, I updated it :).Dearing
At first glance i would say that your code should work.Impulsive
it is working on android 9 and android 11. I'm confuse why is not working on android 10Dearing
Im thinking about the security on android 10 to save on external dirs. but i don't have enough informationDearing
No. For getExternalFilesDir() there is nothing special and you dont need to request read/write permissions or legacy external storage in manifest. Which device? Reboot your device.Impulsive
I tested it on Emulator (Pixel 4 XL) and on Samsung Galaxy S10Dearing
Can you specify which android versions on which devices did you use?Feebleminded
Also, do you have the manifest permission?Feebleminded
@BorisStrandjev I use Huawei Nova 3 (Android 9), Samsung Galaxy S10 (Android 10) , Emulator Pixel 4 XL (Android 10), Emulator Pixel 3a (Android 11). Yes I have <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> and <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" />Dearing
I am not sure an emulator is a good call for such tests. Maybe a real device will also fail on 11. Do you have one at handFeebleminded
@BorisStrandjev as of now, none. we can restrict the app to Android Q only since Android R is not been fully released yetDearing
I disagree. I have a stock Pixel 3a, which is year and a half since released and is running 11 for some time now. Maybe you can try writing your own method to write in the same dir, just to see if you will face issue with that. Is '.Videos' existing already in all your devices? Maybe do a 'mkdirs' just to be sure.Feebleminded
You can set the DestinationInExternalFiles:- request.setAllowedNetworkTypes(DownloadManager.Request). Give the permission of download.Otocyst
T
7

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

Thickset answered 29/1, 2021 at 6:1 Comment(9)
what phone did you use?Dearing
I used this on Emulator, Vivo, Realme, and Samsung smartphones. All were operating on Android 10.Thickset
oh okay, you're from ph :) . Can you paste your whole DownloadManagerRequest? thank youDearing
this works like charm dude! high five. upon my observation, this code is missing ` request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); // Tell on which network you want to download file.` and i use also my path. thank you so muchDearing
have another problem on samsung devices, The notif is not showing but the download is running. how comeDearing
Which device and api?Thickset
Samsung S10, API 29Dearing
I don't have that device, you will have to find the solution for this.Thickset
how can save video using MediaStore for android 11?Gumption
B
0

there is still issue in using request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOCUMENTS, "filename.pdf");

this worked in my case:

    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 + "/"
    }
     

make sure you change Environment.DIRECTORY_PICTURES with your desired directory

Beater answered 25/2, 2021 at 7:18 Comment(0)
W
0

I have this problem in android Q and after a few times, I found this is for its Persian file name. and when I change the name of file, it's fixed.

Weller answered 28/6, 2022 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.