2

I am using DownloadManager class to download file and save it.

DownloadManager.Request request = new DownloadManager.Request(uri);
  request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"logo.jpg");

I am getting "java.lang.IllegalStateException: Unable to create directory: /sdcard/Download" runtime exception.

I have searched all the possible solutions and not able to get the answer.

Below is the full stack tracel .

    E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.architsandesara.myapplication, PID: 24246
    java.lang.IllegalStateException: Unable to create directory: /sdcard/Download
at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:538)
at com.example.architsandesara.myapplication.fragment.DocumentsDetails$2.onClick(DocumentsDetails.java:91)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
srv_sud
  • 647
  • 1
  • 9
  • 26
Archit Sandesara
  • 605
  • 1
  • 12
  • 25

3 Answers3

0

You will have to check if the external getExternalStorageState(). Maybe your App is missing the WRITE_EXTERNAL_STORAGE permission as well.

Jonas Köritz
  • 2,606
  • 21
  • 33
0

secondary storages (like the real removable SD card) are protected by a new permission android.permission.WRITE_MEDIA_STORAGE, and the protection level is signatureOrSystem. one of the answers at stack overflow suggests this.

check this answer aswell, which conveys : From API level 19, Google has added API.

Context.getExternalFilesDirs()
Context.getExternalCacheDirs()
Context.getObbDirs().
srv_sud
  • 647
  • 1
  • 9
  • 26
0

Set local directory like this

            String name = Environment.getExternalStorageDirectory().getAbsolutePath();

            name += "/YourDirectoryName/" ;
Mani kandan
  • 359
  • 4
  • 12