Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 934
bvanvelsen Avatar asked Oct 19 '12 09:10

bvanvelsen


1 Answers

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.

like image 137
abbas.aniefa Avatar answered Sep 25 '22 07:09

abbas.aniefa