androiddownload-manager

download file to custom directory


I am using the DownloadManager to download a file to my Android device. I can choose to download to the DCIM, Downloads, Picutures folder, etc using:

downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.jpg");

This works. But I can only choose a limit set of folders to download to. I want to download to the root of my sdcard using /sdcard/

I tried:

downloadRequest.setDestinationUri(Uri.fromFile(new File("/sdcard/file.jpg")));

But this renders the following exception:

IllegalStateException: android invalid combination of destination: 4, path: /sdcard/file.jpg

How can I do this? I have all the required permissions set.


Solution

  • Try like this.

    downloadRequest.setDestinationInExternalPublicDir("/folderName",file.jpg);
    

    This will create a folder in you external storage root and place the file.jpg in it.