0

Been Googling, followed the documentation. The dialogue doesn't show up at all. I put the uses-permission in the manifest. I don't understand what I am doing wrong. I get to the else branch where it is supposed to launch the permission, but nothing shows up. This seems like such a simple thing but I can't get it to work. Any help would be great.

This is my code from my main activity:

private val requestPermissionLauncher =
        registerForActivityResult(
            ActivityResultContracts.RequestPermission()
        ) { isGranted: Boolean ->
            if (isGranted) {
                Log.d(TAG, "Permission granted.")

            } else {
                Log.d(TAG, "Permission denied.")
            }
        }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        when (PackageManager.PERMISSION_GRANTED) {
            ContextCompat.checkSelfPermission(
                applicationContext,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ) -> {
                Log.d(TAG, "Permission denied.")

                // You can use the API that requires the permission.
            }
            else -> {
                Log.d(TAG, "Permission denied, launcher launched.")

                // You can directly ask for the permission.
                // The registered ActivityResultCallback gets the result of this request.
                requestPermissionLauncher.launch(
                    Manifest.permission.READ_EXTERNAL_STORAGE
                )
            }
        }
    }
Hunter K
  • 13
  • 4
  • 1
    I kept scrolling through Stackoverflow and I found the answer here: https://stackoverflow.com/a/75991649/17683138 You can't READ_EXTERNAL_STORAGE anymore, you have to specify your request. It's called granular permissions. Here is the documentation: https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions – Hunter K Aug 03 '23 at 07:28
  • Have you tried launching the permission checking outside the where? – Simone Aug 03 '23 at 07:29

1 Answers1

0

this permission is depreciated in hign version of Android.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    requestList.add(Manifest.permission.READ_MEDIA_IMAGES)
    requestList.add(Manifest.permission.READ_MEDIA_AUDIO)
    requestList.add(Manifest.permission.READ_MEDIA_VIDEO)
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    requestList.add(Manifest.permission.READ_EXTERNAL_STORAGE)
} else {
    requestList.add(Manifest.permission.READ_EXTERNAL_STORAGE)
    requestList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}

please use this code to build the permissions you need to access external storage.

JeckOnly
  • 347
  • 2
  • 10