Caused by: java.lang.IllegalStateException: Unable to create directory in Android 6.0 devices
Asked Answered
B

3

7

I have to store the download the image from the url using DownloadManager and store it into the sdcard with my own directory like "xyz". This is my code

File img_directory = null;

img_directory = new File(Environment.getExternalStorageDirectory() + "/xyz");
if (!img_directory.exists()) {
    img_directory.mkdirs();
    DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri downloadUri = Uri.parse("my image url");
    DownloadManager.Request request = new DownloadManager.Request(downloadUri);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
      .setAllowedOverRoaming(true)
      .setTitle("Demo")
      .setDescription("Something useful. No, really.")
      .setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
      .setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xyz", image.jpeg);
    mgr.enqueue(request);
}

This code will run upto Android 5.1.1 . When I run this same code in 6.0 its raising the error like this

Caused by: java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/storage/emulated/0/xyz at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:538)

I have added the READ and WRITE permissions in manifest file. How can I resolve this error? Any body can help me ? Thanks in advance.

Bicameral answered 3/3, 2016 at 9:20 Comment(1)
Do you target app for api 23? If so then you will have to ask for write permission on runtime: developer.android.com/training/permissions/requesting.htmlSmutty
C
4
Caused by: java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/storage/emulated/0/xyz at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:538)

Issue seems it be related with Android Runtime Permission introduced in Android 6.0

When your app targeting is API Level 23 or higher, by default all the permissions are false. To resolve this, you have to request a permission dialog and approve the permission before using that into your app.

Cloutier answered 3/3, 2016 at 9:30 Comment(1)
i have given permissions still it is not creating in the pathBurnsed
D
2

As answered above, you need to request the permission you need (in this case, you need the WRITE_EXTERNAL_STORAGE permission), since you are attempting to create a directory in the external storage.

To do so, you need to check that you are either allowed to use that permission or to request it to the user otherwise. This is a sample code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity!!, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 0);
    }
}

As you can see, this is only necessary if the version is equal or higher than M (Android SDK Version 23).

If the user is required to grant permission, then you'll need a way of coming back to your desired point in the code, where you can continue. To do so, you first need to override the onRequestPermissionsResult method, in order to receive the response of the user (if the user decided to grant the requested permission or not):

@Override
public void onRequestPermissionsResult(
        int requestCode, String permissions[], int[] grantResults
) {
    if (requestCode == REQUIRED_PERMISSIONS_REQUEST) {
        int index = 0;
        Map<String, Integer> permissionsMap = new HashMap<>();
        for (String permission : permissions) {
            permissionsMap.put(permission, grantResults[index]);
            index++;
        }

        if (permissionsMap.containsKey(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                && permissionsMap.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == 0) {
            //
            // Call your code from here
            //
        }
    }
}
Dichroism answered 25/10, 2018 at 15:58 Comment(0)
S
0

The problem is that you need write permission to download a file and that is not supported in Instant apps. As you can see in the following link https://developer.android.com/topic/google-play-instant/faqs#general this are the supported permissions in Instant apps.

Which permissions are available to an instant app?

Instant apps can use the following Android permissions:

  • BILLING
  • ACCESS_COARSE_LOCATION
  • ACCESS_FINE_LOCATION
  • ACCESS_NETWORK_STATE
  • CAMERA
  • INSTANT_APP_FOREGROUND_SERVICE only in Android 8.0.
  • INTERNET
  • READ_PHONE_NUMBERS. This permission is available only in Android 8.0 (API level 26).
  • RECORD_AUDIO
  • VIBRATE
Spermatozoon answered 23/9, 2018 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.