Storage permission error in Marshmallow Storage permission error in Marshmallow android android

Storage permission error in Marshmallow


You should be checking if the user has granted permission of external storage by using:

if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {    Log.v(TAG,"Permission is granted");    //File write logic here    return true;}

If not, you need to ask the user to grant your app a permission:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);

Of course these are for marshmallow devices only so you need to check if your app is running on Marshmallow:

 if (Build.VERSION.SDK_INT >= 23) {      //do your check here }

Be also sure that your activity implements OnRequestPermissionResult

The entire permission looks like this:

public  boolean isStoragePermissionGranted() {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)                == PackageManager.PERMISSION_GRANTED) {            Log.v(TAG,"Permission is granted");            return true;        } else {            Log.v(TAG,"Permission is revoked");            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);            return false;        }    }    else { //permission is automatically granted on sdk<23 upon installation        Log.v(TAG,"Permission is granted");        return true;    }}

Permission result callback:

@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {    super.onRequestPermissionsResult(requestCode, permissions, grantResults);    if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);        //resume tasks needing this permission    }}


Android's permission system is one of the biggest security concern all along since those permissions are asked for at install time. Once installed, the application will be able to access all of things granted without any user's acknowledgement what exactly application does with the permission.

Android 6.0 Marshmallow introduces one of the largest changes to the permissions model with the addition of runtime permissions, a new permission model that replaces the existing install time permissions model when you target API 23 and the app is running on an Android 6.0+ device

Courtesy goes to Requesting Permissions at Run Time .

Example

Declare this as Global

private static final int PERMISSION_REQUEST_CODE = 1;

Add this in your onCreate() section

After setContentView(R.layout.your_xml);

 if (Build.VERSION.SDK_INT >= 23)    {        if (checkPermission())        {            // Code for above or equal 23 API Oriented Device             // Your Permission granted already .Do next code        } else {            requestPermission(); // Code for permission        }    }  else    {       // Code for Below 23 API Oriented Device        // Do next code    }

Now adding checkPermission() and requestPermission()

 private boolean checkPermission() {    int result = ContextCompat.checkSelfPermission(Your_Activity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);    if (result == PackageManager.PERMISSION_GRANTED) {        return true;    } else {        return false;    }}private void requestPermission() {    if (ActivityCompat.shouldShowRequestPermissionRationale(Your_Activity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {        Toast.makeText(Your_Activity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();    } else {        ActivityCompat.requestPermissions(Your_Activity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);    }}@Overridepublic void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {    switch (requestCode) {        case PERMISSION_REQUEST_CODE:            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                Log.e("value", "Permission Granted, Now you can use local drive .");            } else {                Log.e("value", "Permission Denied, You cannot use local drive .");            }            break;    }}

FYI

onRequestPermissionsResult

This interface is the contract for receiving the results for permission requests.


Check multiple Permission in API level 23Step 1:

 String[] permissions = new String[]{        Manifest.permission.INTERNET,        Manifest.permission.READ_PHONE_STATE,        Manifest.permission.READ_EXTERNAL_STORAGE,        Manifest.permission.WRITE_EXTERNAL_STORAGE,        Manifest.permission.VIBRATE,        Manifest.permission.RECORD_AUDIO,};

Step 2:

 private boolean checkPermissions() {    int result;    List<String> listPermissionsNeeded = new ArrayList<>();    for (String p : permissions) {        result = ContextCompat.checkSelfPermission(this, p);        if (result != PackageManager.PERMISSION_GRANTED) {            listPermissionsNeeded.add(p);        }    }    if (!listPermissionsNeeded.isEmpty()) {        ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 100);        return false;    }    return true;}

Step 3:

 @Overridepublic void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {    if (requestCode == 100) {        if (grantResults.length > 0                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {            // do something        }        return;    }}

Step 4:in onCreate of Activity checkPermissions();