Richard
Richard

Reputation: 1117

How to check if app has ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION and other storage permissions on lower Androids

Is there a way to check if my device already has the following permissions with one line or how should I check this?

I am supporting SDK versions from 21 to 30 so I cannot use :

if (!Environment.isExternalStorageManager()) {

Because it requires API level 30, but my min is 21.

This is how I am asking for permissions:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        try {

            Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
            Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
            startActivityForResult(intent, 2296);
        } catch (Exception ex){
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
            startActivity(intent);
        }
    } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
    }
}

So is there a permission check that would work for all of these?

Upvotes: 0

Views: 1307

Answers (1)

Richard
Richard

Reputation: 1117

The answer for me here was to change Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q to Build.VERSION.SDK_INT > Build.VERSION_CODES.Q and then check permission with !Environment.isExternalStorageManager().

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
    if(!Environment.isExternalStorageManager()){
        try {
            Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
            Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
            startActivity(intent);
        } catch (Exception ex){
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
            startActivity(intent);
        }
    }
} else {
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
    }
}

Upvotes: 2

Related Questions