kosi
kosi

Reputation: 1

Android 13 keeps denying READ_MEDIA_IMAGES permission even thou I clicked "allow permission". How do I solve this?

I made a READ_MEDIA_IMAGES permission to access pictures from the gallery, but when the request permission dialog pops up, I click "Allow" but on the logcat, the permissions get denied permanently. How do I solve this?

this is my request permission function below

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
    private fun requestImagePermission(){
        Log.i("img permission request","request")

        val permissions = arrayOf(
            //Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_MEDIA_IMAGES
        )

        val pendingPermissions = permissions.filter {
            ContextCompat.checkSelfPermission(requireContext(), it) != PackageManager.PERMISSION_GRANTED
        }.toTypedArray()
        Log.i("img pending permission","$pendingPermissions")

        if (pendingPermissions.isEmpty()) {
            // All permissions already granted, handle the action
            openGallery()
        } else {
            // Request permissions
            requestImagePermissionLauncher.launch(pendingPermissions)
        }
    }


    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    private val requestImagePermissionLauncher =
        registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
            val granted = permissions.all { it.value }
            Log.i("permissions granted", "$granted")
            if (granted) {
                // All permissions are granted, handle the action
                Log.i("img has permission granted","granted")
                openGallery()
            } else {
                Log.i("img permission not granted","not granted")
                // Handle case where not all permissions are granted
                // Permission is denied
                if (
                    ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(),
                        Manifest.permission.READ_MEDIA_IMAGES)) {
                    // Permission denied, but can ask again
                    // Handle this case, show rationale or retry logic
                    Log.i("img permission denied","temporal")
                    requestImagePermission()
                } else {
                    Log.i("img permission denied","permanent")
                    // Permission denied permanently
                    // Handle this case, show settings dialog

                    Utilities.showAlertDialog(requireContext(),
                        title = "Image Permission Required",
                        message = "This permission is needed for you to be able to show the image    of your donation, please grant permission",
                        positiveAction = {
                            AppSettingsDialog.Builder(this).build().show()
                        },
                        negativeAction = {}
                    )
                }
            }
        }

these are all the permissions declared in my manifest file

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
    <uses-permission android:name="android.permission.INTERNET" />

pls, I really need your help as this issue has had me stocked on this project for a long time.

Upvotes: -1

Views: 1023

Answers (1)

Moeed Ahmed
Moeed Ahmed

Reputation: 612

In order to use get permission for android.permission.READ_MEDIA_IMAGES, you must have to take only external storage permission. As per the Android docs says in the link, you only acquire permission android.permission.READ_EXTERNAL_STORAGE

Upvotes: 0

Related Questions