简体   繁体   中英

how to know whether the app currently has the 'All Files Access' permission or not. Android 11

but how to validate if the app currently has the 'All Files Access' permission or not. i use this method to ask permission.

Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse(String.format("package:%s",this.getPackageName())));
            startActivityForResult(intent, 2296);

Please help me to fix this:(

create new dummy 0-bytes file in root of internal memory and then remove it. if both operations success then you have access. when you won't have access then your code will throw Exception (on create-file step), which you can try{ }catch(Exception e) (preventing app crash)

path for internal/external/shared can be obtained from Environment class, I would try at first getExternalStorageDirectory

declare this permission in your manifest.xml :

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

for asking MANAGE_EXTERNAL_STORAGE :

      try {
           Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
           intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
           startActivityForResult(intent, 2296);
          
       } catch (Exception e) {

         Intent intent = new Intent();
         intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
         startActivityForResult(intent, 2296);
       }

get your result in onActivityResult

   @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2296) {
            if (SDK_INT >= Build.VERSION_CODES.R) {
                if (Environment.isExternalStorageManager()) {
                    // perform action when allow permission success
                   
                } else {
                    Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

so, basically you can check permission is granted or not in Android11 like this;

if (SDK_INT >= Build.VERSION_CODES.R) {
   if (Environment.isExternalStorageManager()) {
      // Permission Granted
   }
}

I do it that way:

 public static boolean hasAllFilesAccess(Context context) {

    AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

    try {
        ApplicationInfo app  = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
        return appOpsManager.unsafeCheckOpNoThrow("android:manage_external_storage",app.uid, context.getPackageName()) == AppOpsManager.MODE_ALLOWED;

    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    catch (NoSuchMethodError e)
    {
        return true;
    }

    return false;
}

Seems there is a better way now though:

Environment.isExternalStorageManager()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM