0

I use DownloadManager class to download a file in android

and this is my code for download:

Uri downloadUri = Uri.parse(urlString);
DownloadManager.Request request = new
DownloadManager.Request(downloadUri);
            request.setDescription(des).setTitle(titleAudio).setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED).
                        setDestinationInExternalPublicDir(
                                "/my file name/" , titleAudio + String.valueOf(colForUrl) + pasvand);

                long id = downloadManager.enqueue(request);

                SharedPreferences.Editor prefEdit = preferenceManager.edit();
                prefEdit.putLong(strPref_Download_ID, id);
                prefEdit.commit();

but when I run app in some device(Samsung Galaxy S5) I get this error:

java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/my file name

and this is caused in setDestinationInExternalPublicDir(..)

but in Nexus 7 every thing is right , I have not any problem!!
So, Where is my wrong?!

Mehrdad Faraji
  • 3,744
  • 3
  • 25
  • 38

1 Answers1

1

According to the docs:

setDestinationInExternalPublicDir(String dirType, String subPath)

  • dirType - the directory type to pass to getExternalStoragePublicDirectory(String)
  • subPath - the path within the external directory, including the destination filename

dirType Should be one of DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM. May not be null.

So, for example, try:

setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, titleAudio + String.valueOf(colForUrl) + pasvand);

And go from there if it's not exactly the folder that you want.

MadEqua
  • 1,142
  • 9
  • 19
  • But in nexus 7 I run app and It`s work!! create my directory without any error! I think this is because public storage! in Galaxy S5 and S4 public storage is sd card , but nexus7 don't have any sd card! what do you think?! – Mehrdad Faraji Nov 08 '14 at 14:20
  • Yes, maybe that's part of the problem. Have you set WRITE_EXTERNAL_STORAGE permission on your manifest? – MadEqua Nov 08 '14 at 14:52
  • Yes , So how I can create file in root of directory?! – Mehrdad Faraji Nov 08 '14 at 17:03