Search code examples
androidandroid-permissions

ActivityCompat.requestPermissions Doesn't Show Dialog in Android sdk target 33


Android target SDK Version 33

The permissions are READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE and CAMERA

In the OnCreate method I Calling the ActivityCompat.requestPermissions

@Override
protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(mContext.getResources().getColor(R.color.water_color));
        }

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestStoragePermission();
        }
    }

Following the example proposed here: ActivityCompat.requestPermissions Doesn't Show Dialog in Android

@RequiresApi(api = Build.VERSION_CODES.M)
    private void requestStoragePermission() {

        if (ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
            return;

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
        }

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        }
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                android.Manifest.permission.CAMERA)) {
        }

        ActivityCompat.requestPermissions(this, new String[]
                {
                        android.Manifest.permission.READ_EXTERNAL_STORAGE,
                        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        android.Manifest.permission.CAMERA
                }, STORAGE_PERMISSION);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {


        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == STORAGE_PERMISSION) {
            //If permission is granted
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(mContext, mContext.getString(R.string.permission_granted), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext, mContext.getString(R.string.permission_denied), Toast.LENGTH_SHORT).show();
            }
        }
    }

The Dialog with the permission request does not appear and indeed when the onRequestPermissionsResult method is triggered I only see the toast indicating permission denied

Finally permssion in Manifest file

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

Solution

  • On 33,34 devices you should not ask for WRITE/READ_EXTERNAL_STORAGE anymore.

    You have by default WRITE access to all public directories on external storage.