Pick Multiple Images From Gallery in Kotlin – Android

It’s a common requirement in Mobile Apps to pick an image from the Phone Gallery. Sometimes we also have to pick multiple images at a time from the Gallery. This article explains how you can pick multiple images from the Gallery at a time in Kotlin Android.

If you are looking to pick just one image at a time, then you can visit this simple tutorial, Pick Single Image From Gallery in Kotlin.

pick multiple images from gallery in kotlin

Pick Multiple Images From Gallery in Kotlin

Following is the step by step guide for picking multiple photos from Gallery.

1. ASK User Permissions to Read External Storage

The first step is to add READ_EXTERNAL_STORAGE permission in Your AndroidManifest.xml file. Add the following line above the application tag.

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

For API Level 19 & above You also need to add Runtime permissions. This article will help you to Ask Runtime Permissions in Kotlin Android.

2. Open Phone’s default Gallery App

Now we need to open the phone’s default gallery App to pick images. For that purpose, you can use the following method. For API level below 19 and above 19 the code is slightly different. So both cases are handled here to avoid crashes.

val REQUEST_CODE = 200

private fun openGalleryForImages() {

    if (Build.VERSION.SDK_INT < 19) {
        var intent = Intent()
        intent.type = "image/*"
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
        intent.action = Intent.ACTION_GET_CONTENT
        startActivityForResult(
            Intent.createChooser(intent, "Choose Pictures")
            , REQUEST_CODE
        )
    } 
    else { // For latest versions API LEVEL 19+
        var intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        intent.type = "image/*"
        startActivityForResult(intent, REQUEST_CODE);
    }

}

3. Receive Images in onActivityResult Method

The final step is to override onActivityResult(...) method to receive the data and information about the selected images. For that purpose use the following code.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE){

        // if multiple images are selected
        if (data?.getClipData() != null) {
            var count = data.clipData.itemCount

            for (i in 0..count - 1) {
                var imageUri: Uri = data.clipData.getItemAt(i).uri
           //     iv_image.setImageURI(imageUri) Here you can assign your Image URI to the ImageViews
            }

        } else if (data?.getData() != null) {
            // if single image is selected

            var imageUri: Uri = data.data
         //   iv_image.setImageURI(imageUri) Here you can assign the picked image uri to your imageview

        }
    }
}

As you can see in the code, here we handled 2 cases. If multiple images are chosen by the user the first if part gets executed. Otherwise, if a single photo is picked then the else part will be executed. You can get the Image URIs and can implement your further logic to show images in RecyclerView etc, that part is not shown here. But you can visit this article to read more about RecyclerView.

That’s it. If you have any questions, don’t hesitate to ask in the comments section below.

Visit other Kotlin Articles & Tutorials.

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: