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.

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.

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

 

Download Source code for android select multiple images from gallery example

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...

2 Responses

  1. Joanne says:

    Works Perfectly

Leave a Reply

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