0

In my application, i need to request write and read permission from the storage. Since i want to show the user that the app needs these permissions, i have created and Activity containing a button, which on click, should call the Storage permission Dialog.

However, since the recent Android changes, this doesnt work anymore.

Is there a new (and clean) way to ask permission? Am i doing something wrong?

I have added the uses-permission line inside the AndroidManifest.xml:

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

This is the code inside the Activity:

class ActivityPermission : AppCompatActivity() {

    companion object {
        var PERMISSION_REQUEST_CODE = 12
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityPermissionBinding.inflate(layoutInflater)

        setContentView(R.layout.activity_permission)

        binding.btnPermission.setOnClickListener {
            ActivityCompat.requestPermissions(this, arrayOf(
                    android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE),
                    PERMISSION_REQUEST_CODE)
        }
    }

    override fun onRequestPermissionsResult(
            requestCode: Int,
            permissions: Array<out String>,
            grantResults: IntArray
    ) {
        if (requestCode == PERMISSION_REQUEST_CODE) {
            if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, getString(R.string.permissiongranted), Toast.LENGTH_SHORT).show();
                finish()
            } else {
                Toast.makeText(this, getString(R.string.permissiondenied), Toast.LENGTH_SHORT).show();
            }
        }
    }
}
Meltix
  • 436
  • 6
  • 22
  • 1
    Do you have `WRITE_EXTERNAL_STORAGE` permission in manifest ? if not then add it .Also if you are asking `WRITE_EXTERNAL_STORAGE` you do not have to ask `READ_EXTERNAL_STORAGE` it comes along with Write . – ADM Dec 04 '20 at 12:02
  • @ADM i've just replaced, however sadly the dialog still doesnt show up. Also i get a warning that says that "no longer provides write access when targeting Android 10+". I'll keep it anyway for compatibility reasons. – Meltix Dec 04 '20 at 12:06
  • 1
    Which Android version are you testing on? Note that _"Starting in Android 11, if the user taps Deny for a specific permission more than once during your app's lifetime of installation on a device, the user doesn't see the system permissions dialog if your app requests that permission again. The user's action implies "don't ask again." "_ – Michael Dec 04 '20 at 12:08
  • @ADM i am indeed testing the app in Android 11. I didnt really knew this. I might have to change the way i request permissions then... – Meltix Dec 04 '20 at 12:10
  • reinstall the app and try again . Have a look at [Scoped Storage](https://developer.android.com/guide/topics/permissions/overview#types). – ADM Dec 04 '20 at 12:10
  • @ADM it still doesnt show up, even after reinstalling the app... taking a look at the link you sent – Meltix Dec 04 '20 at 12:12
  • @ADM it is also weird that before the recent Android changes, on an Android R AVD, the permission dialog on button click showed up – Meltix Dec 04 '20 at 12:16
  • 2
    You should be able to determine if the permission dialog was suppressed (i.e. "don't ask again" is in effect) by calling `ActivityCompat.shouldShowRequestPermissionRationale` when you get a denial in `onRequestPermissionsResult`. If this happens you could e.g. try to lead the user to the Android Settings app where they can toggle the permissions for your app. Of course, you should only do this if the permission is absolutely necessary, since the user can percieve this as annoying nagging by your app. – Michael Dec 04 '20 at 12:31

1 Answers1

0

Thanks to everyone for helping me discover more about Android Permissions.

I decided to change the way i ask for the permission. Instead of an Activity, i decided to use a Fragment instead ("show an educational UI to the user. In this UI, describe why the feature, which the user wants to enable, needs a particular permission." - source | Thanks to @Michael in the comments for pointing it out).

Since now im using a Fragment, i have to use requestPermissions (Thanks to this reply). This now works flawlessly without any issues.

Turns out you need a combination of checks when trying to request a permission. You have to first check if the permission is actually enabled with checkSelfPermission, so you can easily choose where the user should go to start using the app.

Meltix
  • 436
  • 6
  • 22