[Solved] The problem of gallery access and gallery opening not working in Android 13 (Kotlin) – Android

by
Ali Hasan
android android-permissions gradle-kotlin-dsl permission-denied

Quick Fix: Update your AndroidManifest.xml to explicitly declare the storage permission:

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

The Problem:

In Android 13, the gallery access and gallery opening functionality is not working as expected. The gallery does not open when the button is clicked, even though a toast message is displayed in the setOnClickListener code. Additionally, the image taken from the gallery cannot be set in the ImageView.

The Solutions:

\n

Solution 1: Fixed inconsistent permissions

\n

The original code uses WRITE_EXTERNAL_STORAGE in the manifest, but the permission checks and request are for READ_EXTERNAL_STORAGE. This needs to be consistent for the program to function correctly.

Solution 2: Use the Android 13 Photo Picker

Android 13 introduced granular media permissions, requiring apps to request specific permissions for accessing media files created by other apps. Instead of using the READ_EXTERNAL_STORAGE permission, which is no longer available, you should use the new Photo Picker API.

The Photo Picker API provides a consistent and user-friendly way to allow users to select photos and videos from their device. To use it, follow these steps:

  1. Declare the READ_MEDIA_IMAGES permission in your manifest file:

    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    
  2. In your code, use the PhotoPicker class to create an intent that opens the photo picker:

    val intent = PhotoPicker.IntentBuilder()
        .build()
    startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE)
    
  3. In the onActivityResult callback, handle the selected media:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK) {
            data?.clipData?.let { clipData ->
                for (i in 0 until clipData.itemCount) {
                    val item = clipData.getItemAt(i)
                    val uri = item.uri
                    // Do something with the URI
                }
            }
        }
    }
    

By using the Photo Picker API, you can comply with Android 13’s privacy requirements and provide users with a seamless experience when selecting photos from their devices.

Q&A

What is the reason behind using Intent.ACTION_PICK for opening the gallery in onActivityResult()?

Intent.ACTION_PICK is used to open the gallery and allow the user to select an image file.

Why is the imageView not updated with the selected image?

The selected image’s URI is obtained in onActivityResult(), but the imageView is not updated with the selected image.

Video Explanation:

The following video, titled "How to Handle Android 13 Permission (Storage Permission doesn't ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

How to Implement Android 13 Permission Handling (Read & Write External Permission is not Work) in Android Studio Kotlin Encountering issues ...