14

I have struggled trying to figure out how the storage system work on Android. Now I am stuck at requesting permission for WRITE_EXTERNAL_STORAGE, and I am using Android 7.1.1. Here is my code:

int check = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (check == PackageManager.PERMISSION_GRANTED) {
            //Do something
        } else {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1024);
        }

UPDATE: So the code does work, it didn't work before because I had a typo in AndroidManifest.xml, thank you for all of your help!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
x1uan
  • 153
  • 1
  • 2
  • 8

2 Answers2

28

Try this,

private Context mContext=YourActivity.this;

private static final int REQUEST = 112;

if (Build.VERSION.SDK_INT >= 23) {
    String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
    if (!hasPermissions(mContext, PERMISSIONS)) {
        ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
    } else {
        //do here
    }
} else {
     //do here
}

get Permissions Result

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //do here
            } else {
                Toast.makeText(mContext, "The app was not allowed to write in your storage", Toast.LENGTH_LONG).show();
            }
        }
    }
}

check permissions for marshmallow

private static boolean hasPermissions(Context context, String... permissions) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

Manifest

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

If you wan't to check multiple permissions at time then add permission into PERMISSIONS array like:

String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.READ_EXTERNAL_STORAGE};
Komal12
  • 3,340
  • 4
  • 16
  • 25
  • After read your code, I think the main difference is the requestPermissions() part, so I tried something like "ActivityCompat.requestPermissions((Activity) MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1024); Not luck. – x1uan Apr 13 '17 at 07:27
  • WHERE did you find the number 122?? I cannot find a list of android requestCodes. – Chud37 Aug 31 '18 at 19:48
  • Sorry 112. Still, I have searched and searched and there is no resource to show requestCodes anywhere :( – Chud37 Aug 31 '18 at 19:53
  • 1
    You can use the number you want. – Roel Oct 08 '18 at 12:01
  • if WRITE_EXTERNAL_STORAGE require user to click (as an approval). What about INTERNET permission? Does it also need to be requested too....? @Komal12 – gumuruh Feb 27 '23 at 04:43
-4

Add permission to your manifest file

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Buben Ivanov
  • 159
  • 3