Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper Way of Requesting WRITE_EXTERNAL_STORAGE Permission [closed]

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!

like image 783
x1uan Avatar asked Apr 13 '17 07:04

x1uan


People also ask

What is the use of manage_external_storage permission?

The MANAGE_EXTERNAL_STORAGE permission allows an application a broad access to external storage in scoped storage. Intended to be used by few apps that need to manage files on behalf of the users. 2. Request External Storage Permissions

Does Android 11 allow write to external storage?

Yes, Android 11 does not allow WRITE_EXTERNAL_STORAGE permission. Thank You for reply. Is there any solution for android 11 to use image picker and get access akso to camera ?

Why can't my Android app access files in external storage?

On Android 11, apps can no longer access files in any other app's dedicated, app-specific directory within external storage. To protect user privacy, on devices that run Android 11 or higher, the system further restricts your app's access to other apps' private directories.

Can I apply permissions to an app without user interaction?

The permission won't be applied automatically to your app without the user's interaction as the user needs to obligatorily grant access to the files. You can do this in Android following these steps:


1 Answers

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};
like image 143
Komal12 Avatar answered Oct 16 '22 00:10

Komal12