Select and Share Multiple Images - Androhub

Multiple Images Banner

Select and Share Multiple Images

Today we are going to learn how to select multiple images from our own created gallery because default gallery doesn’t support us to fetch selected images. So for this we need to create our own gallery and select images and do whatever we want like whatsapp, hike and other app doing.

Another way of doing this thing is to make our app sharable so that we can share multiple images from gallery to our own app.

So today we are going to do both things.

Prerequisite for this tutorial:

  • Custom GridView –  Since we are creating Custom Gallery so we need to create Custom GridView with CheckBoxes.
  • Fetch Images –  To display all images into our GridView then we should know how to fetch images.
  • Shareable Activity – How to create our Activity shareable so that other apps can share multiple images to our activity.

Example

In this tutorial, we are going to create Custom Gallery for selecting multiple images and also how to make our app shareable for multiple images.

Video Demo

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is  MainActivity.java.

2. We are using UniversalImageLoader library for loading images faster and efficiently and you can get the library from the link given. Below are steps to include library:

  • Download the library.
  • Create a new folder under your main project file naming libraries.
  • Copy the library folder from downloaded file and add paste into libraries folder.
  • Now go to your settings.gradle and add the below line and sync your gradle.

  • Now right click on your project and select Open Module Settings -> app -> dependencies -> press “+” icon -> Module dependency -> select your library and press ok.
  • If you want to skip above step then you can go to build.gradle inside app folder and add the below code inside dependencies bracket and sync gradle.

3. Open res ⇒ values ⇒ strings.xml and add below string values. These are some strings that we are going to use in our project.

4. Now, create an xml layout naming activity_main.xml for ShareActivity.java. In this layout i had taken a Button for opening Custom Gallery and GridView for displaying selected images.

5. Create a gridview_layout.xml and add the following code. We create this layout for GridView and include this into other xml files.

6. Now, create a new xml file naming customgallery_activity.xml and add the following code. This layout is for our custom gallery.

7. Since we are using custom gridview so we need create a layout for custom view naming customgridview_item.xml.

8. Now create a java class naming MyApplication.java and extend it with Application. In this class we initialise our ImageLoader library configurations so that we don’t need to it initialise every time but only single time here.

9. Now create a new java class naming CustomGallery_Activity.java and add the following code. In this code we fetch all images in our phone by date wise and load over GridView. If user click on button then our MainActivity will open and display selected images.

10. Create a new custom adapter class for GridView naming GridView_Adapter.java and add the following line. In this we load the passed image over ImageView by using ImageLoader library.

11. Now in MainActivity we will get the selected images from Custom Gallery in onActivityResult() method. Set all image array for loading over gridview.

12. Second thing we are doing if someone share multiple images to our MainActivity then how to get all images the code is shown below:

13. Finally come to your MainActivity.java and add the following code. In this code we are reading images from Custom Gallery and also if someone share images from their app.

14. Now main thing to make our app multiple image shareable we have to add some intent filter inside our activity in AndroidManifest.xml and READ_EXTERNAL_STORAGE permission to read storage data.

And use your MyApplication in application name that we had created in above steps.

15. Now, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

 

46 Comments

Prakhar
Tuesday, December 8th, 2015

Hi m not able to add library could you please help

Droid
Friday, December 11th, 2015

Hi Prakhar,

What error you are facing can you tell me?

kuldeep
Thursday, April 13th, 2017

Hi Friend i need your help.

Dr. Droid
Thursday, April 13th, 2017

Hi Kuldeep,

How can i help you?

Thanks

Sur
Wednesday, October 5th, 2016

I am facing the same problem as well

Vidit
Friday, February 19th, 2016

Thank you so much for this code.
right now i want to upload selected images in mysql database so please help me…!

Droid
Monday, February 22nd, 2016

Hi Vidit,

Please check http://stackoverflow.com/questions/20322528/uploading-images-to-server-android link for image uploading to Server.

Thanks

jafar
Friday, March 11th, 2016

thank you so much for tutorial.
but how to display images from a folder assets android studio project too ?

Droid
Friday, March 11th, 2016

Hi Jafar,

You have to use AssetManager and for help you can check this link – http://stackoverflow.com/questions/8151102/how-to-display-the-image-from-assets-folder-in-android.

Prem Chandran
Friday, July 29th, 2016

Thank you so much for this code! you are awesome. I loved the part where you can just select all items from the Gallery itself, and share load it in Main Activity, which means you can just use this code for those who do not want to build their custom gallery.

Intent intent = new Intent();
intent.setType(“image/*”);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,”Select Picture”), 1);

pradeepkumar reddy
Tuesday, August 2nd, 2016

gridView recycling views is creating problem. if i select an image and scroll down, some other images are selected. how to solve this problem. how to properly handle recycling of views and restore the state of existing views when the gridView is recycling views.

Dr. Droid
Tuesday, August 2nd, 2016

Hi Pradeep Kumar,

For saving selected view instance you have to create a boolean variable and on selection of view set the boolean value to true and on unselection set back to false. For check box use this boolean value for check and uncheck and by-default it will be false for every view.

Thanks

satya
Saturday, August 27th, 2016

How to send multiple images to server php from android

Sur
Wednesday, October 5th, 2016

Could someone specify the path where to copy the libraries with the image loader folder ? it keeps showing error that path not found ?

Dr. Droid
Wednesday, October 5th, 2016

Hi Sur,

Please check this link about how to add libraries in android studio:
https://www.youtube.com/watch?v=1MyBO9z7ojk

Thanks

Fatima
Wednesday, March 1st, 2017

when i try to select image again it deleted the pervious selection ,how saving the pervious images and adding to them?

Dr. Droid
Wednesday, March 1st, 2017

Hi Fatima,

To retain the previously selected images you need to create one Global List of String type and add the selected images into it. Like in OnActivityResult method:

list_name.addAll(new ArrayList(selectedImages));

In this way you can maintain the selected images.

Thanks

Komal Akhani
Wednesday, July 5th, 2017

how to send selected multiple images from gridview to display in other activity .

Dr. Droid
Wednesday, July 5th, 2017

Hi Komal,

You are getting list of selected images in OnActivityResult same you can pass to another activity.
You can check the below link to send ArrayList between two activities:
https://stackoverflow.com/questions/21250339/how-to-pass-arraylistcustomeobject-from-one-activity-to-another

Thanks

Alok Kumar
Friday, July 21st, 2017

Could you please look into this problem and help me to resolve it? Link is described below

https://stackoverflow.com/questions/45231157/custom-video-gallery-with-multiple-selection-system-and-populating-those-to-the

shivam goyal
Friday, July 28th, 2017

there is an error while adding library
Configuration with name ‘Default’ not found

Stan
Friday, November 10th, 2017

Thank you, but i am getting an error while running the app in the CustomGallery_Activity.

This is where the application gets closed:
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy + ” DESC”);//get all data in Cursor by sorting in DESC order

Do You have a suggestion how to solve it?

Thanks!

Dr. Droid
Friday, November 10th, 2017

Hi Stan,

Can you tell me what exception you are getting?

If you are running the app in OS Version 6.0+ then you have to implement run time permission.
You can see this link for implementing run time permission : https://www.androhub.com/working-android-marshmallow-6-0-permissions/

Thanks

Stan
Thursday, November 16th, 2017

Work fine now!
Thank you for your help, Dr Droid!

Have a nice day

Stan

sumanth
Saturday, December 16th, 2017

hi,
i am getting this error.
Error:(45, 0) Could not read script ‘C:\Users\sri sumanth\Desktop\Temp3\libraries\gradle\maven_push.gradle’ as it does not exist.
Open File

Dr. Droid
Saturday, December 16th, 2017

Hi Sumanth,

Please see the below link:

https://stackoverflow.com/questions/28695045/how-does-android-studio-import-gradle-projects

Thanks

sumanth
Tuesday, December 19th, 2017

error ::
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.srisumanth.temp3, PID: 6513
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.srisumanth.temp3/com.example.srisumanth.temp3.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.constraint.LinearLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.constraint.LinearLayout
at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.srisumanth.temp3.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 
at android.app.ActivityThread.access$900(ActivityThread.java:153) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5438) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) 
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.constraint.LinearLayout
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
at com.example.srisumanth.temp3.MainActivity.onCreate(MainActivity.java:28) 
at android.app.Activity.performCreate(Activity.java:6303) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 
at android.app.ActivityThread.access$900(ActivityThread.java:153) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5438) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) 
Caused by: java.lang.ClassNotFoundException: Didn’t find class “android.support.constraint.LinearLayout” on path: DexPathList[[zip file “/data/app/com.example.srisumanth.temp3-2/base.apk”],nativeLibraryDirectories=[/data/app/com.example.srisumanth.temp3-2/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:496) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
at com.example.srisumanth.temp3.MainActivity.onCreate(MainActivity.java:28) 
at android.app.Activity.performCreate(Activity.java:6303) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 
at android.app.ActivityThread.access$900(ActivityThread.java:153) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5438) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) 
Suppressed: java.lang.ClassNotFoundException: android.support.constraint.LinearLayout
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
… 22 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

CODE::

Dr. Droid
Tuesday, December 19th, 2017

Hi Sumanth,

The error is in your xml layout. You are using android.support.constraint.LinearLayout element which does not exist in Android. It should be only LinearLayout or android.support.constraint.ConstraintLayout.

Thanks

sumanth
Tuesday, December 19th, 2017

After changing it to linear layout,
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.srisumanth.temp3/com.example.srisumanth.temp3.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class Gridview…
this error is popped out.

Dr. Droid
Tuesday, December 19th, 2017

Hi Sumanth,

Can you share your xml layout code with me at my mail id.

Thanks

Ash
Sunday, February 4th, 2018

Hi, nice tutorial and it helps a lot on my project. I must say brilliant..

I just want to ask you, I kept getting error on this line:

ImageLoader.getInstance().displayImage(“file://” + imageUrls.get(position), imageView, options);

Because what I understand the ImageLoader must be init with configuration before using right? – correct if I’m wrong.
I think I follow all phase by phase step by step.

Looking forward for your reply.

Thank You 🙂

Dr. Droid
Sunday, February 4th, 2018

Hi Ash,

First of all thank you 🙂

And yes you are right that you have to init ImageLoader before using it. If you see MyApplication.java class there i am initialising the ImageLoader and MyApplication.java is declaring in Manifest file.

Thanks

Ash
Sunday, February 4th, 2018

Thanks for your reply sir..yeah I overlooked….shame on me….the manifest….Can you give me suggestions how can I make this multiple picking images custom gallery but can picking a multiple video too.. 🙂 …I’m sorry. too much questions.. because I tried other library, but it cant work flawlessly as yours.

Dr. Droid
Monday, February 5th, 2018

Hi Ash,

Actually i haven’t done any article on Video Listing but i am sharing you one link just go through it.

https://stackoverflow.com/questions/20694047/get-images-and-videos-from-android-phone-into-custom-gallery

Thanks

Jeewan
Wednesday, February 21st, 2018

Hello Sir, plz help me out I don’t understand which library folder I have to download. Either I have to download the entire github project and paste it into the library directory or something else.

Dr. Droid
Wednesday, February 21st, 2018

Hi Jeewan,

You have to download the whole github project but you have to copy only “library” folder from downloaded one and paste it into your Project library directory.

Thanks

cinthia
Tuesday, March 6th, 2018

Hello, Im using UniversalImageLoader library in my app in SO 8.0 when i select to take a image from gallery my app is close and i get this exception java null exception in so 5.1 and 6 work perfect

Dr. Droid
Wednesday, March 7th, 2018

Hi Cinthia,

I think you have to implement latest FileProvider to make it work in Android 8.0 devices. I had implemented this in one of my article here is the link : https://www.androhub.com/android-compose-tweets-twitter/

For official documentation : https://developer.android.com/reference/android/support/v4/content/FileProvider.html

File provider will work for all the API levels.

Thanks

SM Sakif Sadad Dil
Saturday, March 23rd, 2019

How can I upload this images to Firebase Storage??

Dr. Droid
Saturday, March 23rd, 2019

Hi SM,

For this you have to go through firebase storage documentation.

Thanks

Amine
Thursday, May 9th, 2019

hello and thank you for your work,

is there a way to add a button to the activity so that you can share the selected images? like in a normal gallery app where you get to share selected pictures?

Dr. Droid
Thursday, May 9th, 2019

Hi Amine,

Yes there is a way to share selected images by using a button. For this you should know how to share the multiple images.
Check this link.

Thanks

Jenika
Monday, July 27th, 2020

Hello sir, I am quite new at android studio. I am getting an error in the CustomGallery_Activity.

At:
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy + ” DESC”);//get all data in Cursor by sorting in DESC order

It says that:
managedQuery(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)’ is deprecated as of API 16: Android 4.1 (Jelly Bean)

I know that someone else asked you the same query earlier but I didn’t really get the solution to it. Sorry for repeating this question. Hope you can help me out.

Dr. Droid
Wednesday, July 29th, 2020

Hi Jenika,

Are you getting any error or code the doesn’t give you the desired result?
Deprecate is just a warning and you can ignore it for now if the code works fine.

Thanks

Post comment

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