[android] Android 6.0 Marshmallow. Cannot write to SD Card

I have an app that uses external storage to store photographs. As required, in its manifest, the following permissions are requested

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

and it uses the following to retrieve the required directory

File sdDir = Environment

SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd", Locale.US);
String date = dateFormat.format(new Date());
storageDir = new File(sdDir, getResources().getString(
            + "-" + date);

// Create directory, error handling
if (!storageDir.exists() && !storageDir.mkdirs()) {
 ... fails here

The app works fine on Android 5.1 to 2.3; it has been on Google Play for over a year.

Following an upgrade of one of my testing phones (Android One) to 6, it's now returning an error when trying to create the requisite directory, "/sdcard/Pictures/myapp-yy-mm".

The sd card is configured as "Portable storage". I've formatted the sd card. I've replaced it. I've rebooted. All to no avail.

Also, the built-in android screenshot functionality (via Power+Lower volume) is failing "due to limited storage space, or it isn't allowed by the app or your organisation".

Any ideas?

This question is related to android android-sdcard android-6.0-marshmallow

The answer is

I faced the same problem. There are two types of permissions in Android:

  • Dangerous (access to contacts, write to external storage...)
  • Normal

Normal permissions are automatically approved by Android while dangerous permissions need to be approved by Android users.

Here is the strategy to get dangerous permissions in Android 6.0

  1. Check if you have the permission granted
  2. If your app is already granted the permission, go ahead and perform normally.
  3. If your app doesn't have the permission yet, ask for user to approve
  4. Listen to user approval in onRequestPermissionsResult

Here is my case: I need to write to external storage.

First, I check if I have the permission:

private static final int REQUEST_WRITE_STORAGE = 112;
boolean hasPermission = (ContextCompat.checkSelfPermission(activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},

Then check the user's approval:

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode)
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                //reload my activity with permission granted or use the features what required the permission
            } else
                Toast.makeText(parentActivity, "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();


You can read more about the new permission model here: https://developer.android.com/training/permissions/requesting.html

Similar questions with android tag:

Similar questions with android-sdcard tag:

Similar questions with android-6.0-marshmallow tag: