Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DownloadManager fails to download to external storage on Samsung with Pie

Downloading a file using the Android DownloadManager to the external storage fails on Samsung Galaxy S9, S9+ on Android 9 (Pie).

Download works for Samsung Devices with Android 8 or other devices with Android 9( e.g. Pixel 2)

I have added to the manifest the following permissions:

 "android.permission.WRITE_EXTERNAL_STORAGE", 
 "android.permission.READ_EXTERNAL_STORAGE",
 "android.permission.INTERNET"

I have also requested READ/WRITE_EXTERNAL_STORAGE permissions at runtime.

The file path is: /storage/4295-DDD5/Android/data/com.example.myapplication/files/filename.file and this filepath exists, I have created it using the Device File Explorer of AndroidStudio

The method which creates a download:

public void downloadFile(String url, String filePath) {
   DownloadManager mDownloadManager = 
   (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);

   DownloadManager.Request request = new 
   DownloadManager.Request(Uri.parse(url));

   request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | 
   DownloadManager.Request.NETWORK_WIFI);

   request.setVisibleInDownloadsUi(true);

   request.setDestinationUri(Uri.parse("file://" + filePath));

   mDownloadManager.enqueue(request);
}

The expected result is the download of the specified file from the url to the filePath instead I am getting the following errors in logs:

D/DownloadManager: [8616] Starting com.example.myapplication
W/DownloadManager: [8616] Stop requested with status FILE_ERROR: Failed to generate filename: java.io.IOException: Permission denied
D/DownloadManager: [8616] Finished with status WAITING_TO_RETRY
V/DownloadManager: MIME Type = application/octet-stream
I/DownloadManager: Download 8616 finished with status WAITING_TO_RETRY
I/DownloadManager: Notification Clear Download 1:com.example.myapplication
like image 850
Oagar Avatar asked Apr 09 '19 11:04

Oagar


People also ask

How do I fix Download Manager on Android?

- Select Download Manager in settings - apps, force stop, storage and clear STORAGE. - Select Play Store in settings apps, force stop, storage, clear CACHE. - Restart the phone and see if the problem persists.


1 Answers

Oagar, what have helped me in the past is reading all 8 "The Death of External Storage" blog posts from https://commonsware.com/blog/archive.html

I think the root cause is hiding in this:

The file path is: /storage/4295-DDD5/Android/data/com.example.myapplication/files/filename.file and this filepath exists, I have created it using the Device File Explorer of AndroidStudio

Just because you have created this directory and see it doesn't mean your app is able to write into it. So try writing somewhere you know for sure you can write files.

Hopefully this will help you.

like image 154
Alex Bravo Avatar answered Sep 22 '22 16:09

Alex Bravo