java.lang.IllegalStateException: Not one of standard directories: /storage/emulated/0/Download/ on Android 10

Viewed 10013

I Want To Download And save file from my url to external storage download directory in Android 10. I have used this code to download.

    public void StartNewDownload(String url) {

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDescription("Downloading Profile"); 
    request.setTitle("Abc App");
    request.setDestinationInExternalPublicDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+ File.separator, "parag.jpeg"); 

    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    final long downloadId = manager.enqueue(request); 

}

it shows error

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.abc, PID: 15197

java.lang.IllegalStateException: Not one of standard directories: /storage/emulated/0/Download/
    at android.os.Parcel.createException(Parcel.java:2079)
    at android.os.Parcel.readException(Parcel.java:2039)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
    at android.content.ContentProviderProxy.call(ContentProviderNative.java:658)
    at android.content.ContentProviderClient.call(ContentProviderClient.java:558)
    at android.content.ContentProviderClient.call(ContentProviderClient.java:546)
    at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:569)
    at com.example.abc.Activity.showDetails.StartNewDownload(showDetails.java:102)
    at com.example.abc.Activity.showDetails$1.onClick(showDetails.java:68)

But if i use

request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS+ File.separator , "parag.jpeg");

this downloads the file but in my app specific folder but i want to download the file in external public download directory

4 Answers

as per your code getExternalStoragePublicDirectory API is deprecated.

use the below code to save the file in the standard directory.

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,"parag.jpeg")

it will try to save to the standard directory

/storage/emulated/0/Android/data/packagename/files/Pictures/parag.jpg

For applications targeting Build.VERSION_CODES.Q or above, WRITE_EXTERNAL_STORAGE permission is not needed and the dirType must be one of the known public directories like Environment#DIRECTORY_DOWNLOADS, Environment#DIRECTORY_PICTURES, Environment#DIRECTORY_MOVIES, etc.

I hope this helps.

First of all add this line to your AndroidManifest.xml file inside application tag.

android:requestLegacyExternalStorage="true"

Use Environment.DIRECTORY_PICTURES or any Environment that Appropriate to your file.

request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE
        )
            .setAllowedOverRoaming(false).setTitle("Krishan Demo") //Download Manager Title
            .setDescription("Downloading...") // Download manager Discription
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            .setDestinationInExternalPublicDir(
                Environment.DIRECTORY_PICTURES, // It can be any stanaderd directory Like DCIM,Downloads etc...
                "/KrishanImages/testing.jpg" // Your Custom directory name/Your Image file name
            )

You can use the code below to save the file.

request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS, fileType)

It Work for Me

in manifest
android:requestLegacyExternalStorage="true"
For Android below v28 and v28+

  String folder = "/Download";
                    try { //V28 Below
                        request.setDestinationInExternalPublicDir(folder, filename);//v 28 allow to create and it deprecated method(v28+)

                    } catch (Exception e) {

                        //For Android  28+
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);//(Environment.DIRECTORY_PICTURES,"picname.jpeg")
                    }
Related