0

How to call listener on user click disable/allowed WRITE_EXTERNAL_STORAGE permission ?

I want to call listener on user click deny giving WRITE_EXTERNAL_STORAGE permission

inkredusk
  • 919
  • 4
  • 16
  • I'm not sure what you're asking. Do you have a button that you want users to click, that will then prompt them to change the WRITE_EXTERNAL_STORAGE permission? – JonR85 Apr 22 '23 at 14:12
  • @JonR85 I want to do something, during a permission request dialog session, when the user clicks allow or deny. – Fiqih Hasroni Apr 22 '23 at 20:54

1 Answers1

0

First I would suggest reading the post here: WRITE_EXTERNAL_STORAGE when targeting Android 10

TL:DR - Write to external storage is deprecated and doesn't work in SDK 30+

Also read this from Android.com: https://developer.android.com/about/versions/11/privacy/storage

Now the answer to your question.

You want to override the onRequestPermissionsResult() method. In your activity do something like this.

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

    if (requestCode == WRITE_EXTERNAL_STORAGE) {// If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this, "Write External Storage Granted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "Write External Storage Denied", Toast.LENGTH_SHORT).show();
         }
    }
}
JonR85
  • 700
  • 4
  • 12