How to solve write permission error on Android Marshamallow (API 24)? [SOLVED] – Android Development

Since Android 6.0 (Marshmallow), Google has changed the way permissions used to work.
Earlier the user granted permissions read/write permission to the app while installing it. Once the app was installed, the user could not change it’s permissions.

However, since Marshmallow, the apps aren’t granted the read/write permissions on install. Instead the apps require asking the user to grant the permissions at run-time. The situation is the same for Nougat too.
Moreover, users can now go to app settings and change the apps permissions whenever they want.

But you don’t need to worry as the solution is simple.

Note: In this tutorial I will be using the WRITE_EXTERNAL_STORAGE permission as it is implicitly granted the READ_EXTERNAL_STORAGE. If you only want to have the READ_EXTERNAL_STORAGE then just replace WRITE with READ. If you want to have both then WRITE permission will give you both.

First you need to check if the permissions are granted or not. To do that add the following code:

public  boolean isStoragePermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            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;

The above method/function checks whether the permissions are granted or not and returns true/false accordingly, it also requests the permission if the android version is Marshmallow or above.
So whenever you need to access the storage just call this function and perform the tasks requiring the permissions if the function returns true.
This can be achieved as shown below using an if-statement:

        if (isStoragePermissionGranted()){
//perform the task requiring permissions
        else if(Build.VERSION.SDK_INT < 23){
            Toast.makeText(getBaseContext(),"Please enable storage permission from settings",Toast.LENGTH_LONG).show();

What this would do is, that it would check if the permission is granted or not and request the permissions if they aren’t granted.
But there is one thing you should keep in mind. Let’s say user clicks a button to save something on their phone and you use the above code to perform the task. Now, if the permission isn’t already granted then the request to permission will pop up and user would be able to allow or deny it. Let’s say the user allows it. Now once the permission is granted the tasks inside the if-statement would not run automatically, and instead the user will have to click on the button again, and now that the user has allowed the permission, the task will be executed. So the user will have to click on the button twice the first time.
If you want some task to be executed as soon as the permission is granted, then you can add the following method:

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

The above method will be called as soon as the user hits ‘allow’ and the task will be executed.

and yeah, don’t forget to add the following permission to your AndroidManifest.xml

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


[wpedon id="7041" align="center"]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.