Android Select Multiple Images From Gallery Programmatically Show In GridView

Android select multiple images from gallery Programmatically tutorial example is for you androiders.

In this example, we will pick/get the multiple images from gallery and we will show them in a gridview.

We will choose/take multiple photos from gallery in the Android Studio by opening via Gallery Intent.

You can also select single image in this tutorial.

If you want to pick single image from gallery or camera then, read the android pick image from gallery or camera example.

Last Images of app

The output of this tutorial is look like below images.

android select multiple images from gallery

First Screen

android select multiple images from gallery

Second Screen

android select multiple images from gallery

Third Screen

android select multiple images from gallery

Fourth Screen

Step 1. Making Layout

By default you have activity_main.xml layout file.

Add following source code into it.

I have taken one button and one gridview in this layout file.

When the user will click the button, it will open all the images available on the android device.

It will let the user choose one or multiple images from here.

When the user finishes picking up the images, all the selected images will be shown in the gridview.

Step 2. Java Coding

Now Add below source code in to MainActivity.java file

Now let us understand what output will we have when the above code is run by the compiler.

When the user will click on the button, below code will be run.

I have created one intent object here.

This intent will call the activity which will fetch all the images from android devices.

User will select necessary images from here and when he completes this task, he will be taken back to our app.

When compiler comes back to our app, it also takes selected images with it.

At this time, onActivityResult() method is called and these images are available in this method.

Take a look at the code structure of onActivityResult() method.

All the core logic is present in the above source code.

If user choose single image

When compiler comes to this code, first it will check whether user have selected single image or multiple image.

If user have picked up single image then compiler will run below code

Here, we will have the data of the selected image in the form of URI.

We can set the image in the imageview by using this URI.

After retrieving the image, compiler will set the gridview with this image.

I will describe the code for gridview and it’s adapter in the last chapter of this tutorial.

When the user have selected multiple images

Now if the user have selected multiple images then the below code will be initiated.

Compiler will make one arraylist which contains all the URIs of all the selected images.

One for loop is initiated here. In the every iteration of this for loop, URI of the image is added into the arraylist.

The number of iterations of the for loop equal to the number of selected images, means that if the user have taken three images then the for loop will have three iterations.

This arraylist is then used to set up the gridview.

Step 3. Setting up the GridView

Before setting up the gridview, first we need to create one layout resource file.

This layout resource file (gv_item.xml) will represent the every single cell of the gridview.

Code for gv_item.xml

Every gridview implementation requires the adapter class which will provide necessary data to the gridview.

Here, I have written GalleryAdapter.java class for this purpose.

Source code for GalleryAdapter.java is as following

We are passing an arraylist of image URIs in the second parameter of the constructor.

Compiler will use this arraylist to set up the image in the imageview.

Look at the getView() method in the adapter class. Image is set up with the URI in this method.

All the technical aspects for android select multiple images from gallery example is over now

Download Source code for android select multiple images from gallery example

[sociallocker]Click to Download Source Code For SelectMultipleImagesGallery[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

39 Responses

  1. Joanne says:

    Works Perfectly

  2. TEST says:

    BUT CANNOT GET IMAGE ORIGINAL PATH

  3. TEST says:

    cannot get image original file path

  4. test says:

    how to upload mutliple image file in android

  5. John says:

    I have one question . i add some image files to the grid view but when i click select multiple images again , the previous ones dissapear . is it possible to keep the old ones too ?

  6. John says:

    Greetings ! when i insert multiple photos to the grid and i click to add photos again the previous ones dissapear . is it possible to keep the old ones too ?

  7. Rahman says:

    When i click for the second image to select the app close down. Can you tell me whats i am doing wrong here ? I am using android emulator.

    • Hardik Parsania says:

      I have tested it twice on different devices. I am able to select multiple images.
      What error are you getting when app is crashing?

  8. Rajshree says:

    Hello Hardik, it is really very helpful example , one issue i am facing with this that when i select images from drive or photos then i get only 1 image on the place of 2-3 images, please suggest me for this.

    Thanks.

  9. Ajay says:

    hello Hardik, when I selected multiple images, the data.getClipData() showing null and showing you haven’t picked image…while for single selection of image it properly running…can you help me out of this.

    Thank you.

  10. adeel says:

    is it possible we get image from camera and set in same gridview ?

  11. sagarika says:

    Hi..
    Iam not able to get image original path from filePathColumn array

  12. slimTouati says:

    Hello , thanks for the amazing tutorial
    I tried to get the original path in order to upload the pictures on my server , however i got a null pointer exception in the filePathColumn array.. please answer as soon as you see my comment , it’s for my university project and i’m stuck

    • Hardik Parsania says:

      Can you post your logcat?
      I suggest you to download source code and run on your device and check if it’s work or not?

  13. Jeevanchand Lankireddy says:

    hi,

    can i know how to implement the same using horizontal recycler view ?

  14. Jeevanchand Lankireddy says:

    Hi ,

    can i implement the same using horizontal recycler view ? i want the images to be in horizontal with a scroll bar of max 5 images , how can i implement with horizontal recyclerview ? Thank you for your help on this.

    • Hardik Parsania says:

      you need to replace gridview with horizontal recyclerview. Replace XML code, adapter class and in MainActivity.java

      • Jeevanchand Lankireddy says:

        can you please help me with the code to choose max 5 images and it should not allow me to choose 6th image from gallery ? Thank you for your help on this.

  15. Jeevanchand Lankireddy says:

    i am new to java as well android , if you can help me , i will be glad.

  16. Zerina says:

    If the gridview is showing only one selected photo then delete if(data.getData()!=null){..}, It worked for me. I can select one and more than one photo.

  17. amine says:

    hi sir !
    it s a great tuto , but i want to know if it is possible to use this in Fragment ?

Leave a Reply

Your email address will not be published. Required fields are marked *