Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 582
Parag Wadhwani Avatar asked Mar 09 '20 09:03

Parag Wadhwani


2 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.

like image 128
jose praveen Avatar answered Sep 21 '22 09:09

jose praveen


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
            )
like image 35
Mohamed Ben Romdhane Avatar answered Sep 22 '22 09:09

Mohamed Ben Romdhane