Proper Way of Requesting WRITE_EXTERNAL_STORAGE Permission [closed]
Asked Answered
T

2

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!

Tania answered 13/4, 2017 at 7:17 Comment(9)
Have you mentioned permission for WRITE_EXTERNAL_STORAGE in your manifest file ?Mcfarland
I absoltely have, I wrote something like "<user-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />"Tania
This works well codereview.stackexchange.com/questions/143769/…Quattlebaum
permission should be <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>Haletta
@sapanravani Isn't your code the same as mine?Tania
@Tania you have written user-permission in place of uses-permissionHaletta
try this #33666571Wickliffe
@sapan You are absolutely right! OMG, thank you so much! I almost killed myselft.Tania
@Tania most welcome...:)Haletta
M
-4

Add permission to your manifest file

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>
Molokai answered 13/4, 2017 at 7:21 Comment(7)
I did, <user-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />Tania
Problem solved, apparently, I should use <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />Tania
The other answer is correct, you have to add run time permission Too.Methodical
Marshmallow onwards you need to add runtime permission. This cannot be the accepted answer!Artemas
we require run time permission tooSiddon
where is runtime permission @FaisalShaikh ?Remittance
@Remittance https://mcmap.net/q/806435/-proper-way-of-requesting-write_external_storage-permission-closedSiddon
B
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};
Burnedout answered 13/4, 2017 at 7:21 Comment(5)
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.Tania
WHERE did you find the number 122?? I cannot find a list of android requestCodes.Leveller
Sorry 112. Still, I have searched and searched and there is no resource to show requestCodes anywhere :(Leveller
You can use the number you want.Dibbuk
if WRITE_EXTERNAL_STORAGE require user to click (as an approval). What about INTERNET permission? Does it also need to be requested too....? @BurnedoutRemittance
M
-4

Add permission to your manifest file

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>
Molokai answered 13/4, 2017 at 7:21 Comment(7)
I did, <user-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />Tania
Problem solved, apparently, I should use <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />Tania
The other answer is correct, you have to add run time permission Too.Methodical
Marshmallow onwards you need to add runtime permission. This cannot be the accepted answer!Artemas
we require run time permission tooSiddon
where is runtime permission @FaisalShaikh ?Remittance
@Remittance https://mcmap.net/q/806435/-proper-way-of-requesting-write_external_storage-permission-closedSiddon

© 2022 - 2024 — McMap. All rights reserved.