1

I want to download file to public dir, it works well when sdcard is avaialble but gives me above error when sdcard is not avaiable. I do checking of sdcard. I want to save files to DIRECTORY_MUSIC which is public by default. But the line request.setDestinationInExternalFilesDir() gives me above error.

Here is the code so far I have written

File dir = new File(Environment
                .DIRECTORY_MUSIC + "/" + DIR_NAME + "/");
        if (!dir.exists()) {
            // create dir for first time
            Log.d(LOG_TAG, "first time created dir");
            dir.mkdir();
        }

        DownloadManager dm = (DownloadManager) v.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
        Uri songLink = Uri.parse(streamUrl);
        DownloadManager.Request request = new DownloadManager.Request(songLink);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false)
                .setTitle(title)
                .setMimeType("audio/mp3")
                .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                .setDestinationInExternalFilesDir(v.getContext(), dir.getAbsolutePath(),
                        File.separator + DIR_NAME + File.separator + title);

        dm.enqueue(request);

What changes should I do to save file in that dir

I want to save music file which I am downloading from an url and saving to public music directory so that music player will find that file

My question is how Whatsapp make WhatsApp audio folder and download the whatsapp audios to that dir. For phones with SD card I can do this but I fail when there's no SD card in phone. How it is done ?

8
  • dir.mkdir();. You should check the return value. Or check again if the directory exists after that. And not continue if it does not.
    – greenapps
    Apr 10, 2016 at 7:10
  • There is no data directory in external files dir. Nor are you creating that directory. What do you have in mind exactly concerning the final path? So about which dir are you talking?
    – greenapps
    Apr 10, 2016 at 7:12
  • setDestinationInExternalFilesDir(v.getContext(), dir.getAbsolutePath(), File.separator + DIR_NAME + File.separator + title);. Please tell in your post the exact values of the used parameters. We cannot see what those paths are. (In your post. Not in a comment). Also the exact path of the dir you tried to create.
    – greenapps
    Apr 10, 2016 at 7:17
  • setDestinationInExternalFilesDir. ?? What would that have to do with a music directory?
    – greenapps
    Apr 10, 2016 at 7:21
  • Has DownloadManager a different concept of 'external files dir' than we?
    – greenapps
    Apr 10, 2016 at 7:22

1 Answer 1

0

To save download file in public directory:

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, filename);

This method throws:

Throws IllegalStateException If the external storage directory cannot be found or create

So you must check if external storage is writable before use.

public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

Also requires permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

More info

2
  • the method returns false... so I cant download file to public dir?
    – sap
    Apr 10, 2016 at 6:28
  • You can test it :D But I think it's impossible. Let's show message request user insert External storage or stop unmount External storage (when plug usb cable to computer, External storage is unmout)
    – Khang .NT
    Apr 10, 2016 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.