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:
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.
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:
1 |
include ':libraries:UniversalImageLoader-Library' |
1 |
compile project(':libraries:library') |
3. Open res ⇒ values ⇒ strings.xml and add below string values. These are some strings that we are going to use in our project.
1 2 3 4 5 6 7 8 |
<resources> <string name="app_name">Select MultipleImages Demo</string> <string name="customGallery">Custom Gallery</string> <string name="mainTitle">Select MultipleImages From Gallery</string> <string name="openCustomGallery">Open Custom Gallery</string> <string name="selectedImages">Selected Images will display below</string> </resources> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#eeeeee" android:orientation="vertical" android:padding="5dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:padding="10dp" android:text="@string/mainTitle" android:textColor="#00BCD4" android:textSize="18sp" /> <!-- Button to Open Custom gallery --> <Button android:id="@+id/openCustomGallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#ffffff" android:text="@string/openCustomGallery" android:textColor="#009688" android:textSize="15sp" /> <View android:layout_width="fill_parent" android:layout_height="1px" android:layout_marginTop="5dp" android:background="#616161" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:padding="5dp" android:text="@string/selectedImages" android:textColor="#9e9e9e" android:textSize="14sp" /> <!-- Grid View to display Selected Images--> <include android:id="@+id/selectedImagesGridView" layout="@layout/gridview_layout" /> </LinearLayout> |
5. Create a gridview_layout.xml and add the following code. We create this layout for GridView and include this into other xml files.
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#eeeeee" android:horizontalSpacing="4dp" android:listSelector="@android:color/transparent" android:numColumns="2" android:verticalSpacing="4dp" /> |
6. Now, create a new xml file naming customgallery_activity.xml and add the following code. This layout is for our custom gallery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#eeeeee" android:padding="5dp"> <!-- Grid View fr Custom Gallery --> <include android:id="@+id/galleryImagesGridView" layout="@layout/gridview_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/selectImagesBtn" /> <!-- Button to return Selected Images --> <Button android:id="@+id/selectImagesBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginTop="5dp" android:background="#ffffff" android:textColor="#009688" android:textSize="15sp" android:visibility="gone"/> </RelativeLayout> |
7. Since we are using custom gridview so we need create a layout for custom view naming customgridview_item.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:layout_margin="3dp"> <ImageView android:id="@+id/galleryImageView" android:layout_width="140dp" android:layout_height="170dp" android:src="@mipmap/ic_launcher" android:scaleType="centerCrop"/> <CheckBox android:id="@+id/selectCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/galleryImageView" android:layout_alignParentRight="true" android:layout_centerVertical="true"/> </RelativeLayout> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.selectmultipleimages_demo; import android.app.Application; import android.content.Context; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.assist.QueueProcessingType; /** * Created by SONU on 31/10/15. */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); initImageLoader(getApplicationContext()); } //Initiate Image Loader Configuration public static void initImageLoader(Context context) { ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder( context); config.threadPriority(Thread.NORM_PRIORITY - 2); config.denyCacheImageMultipleSizesInMemory(); config.diskCacheFileNameGenerator(new Md5FileNameGenerator()); config.diskCacheSize(50 * 1024 * 1024); // 50 MiB config.tasksProcessingOrder(QueueProcessingType.LIFO); config.writeDebugLogs(); // Remove for release app // Initialize ImageLoader with configuration. ImageLoader.getInstance().init(config.build()); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
package com.selectmultipleimages_demo; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.GridView; import java.util.ArrayList; /** * Created by SONU on 31/10/15. */ public class CustomGallery_Activity extends AppCompatActivity implements View.OnClickListener { private static Button selectImages; private static GridView galleryImagesGridView; private static ArrayList<String> galleryImageUrls; private static GridView_Adapter imagesAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.customgallery_activity); initViews(); setListeners(); fetchGalleryImages(); setUpGridView(); } //Init all views private void initViews() { selectImages = (Button) findViewById(R.id.selectImagesBtn); galleryImagesGridView = (GridView) findViewById(R.id.galleryImagesGridView); } //fetch all images from gallery private void fetchGalleryImages() { final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};//get all columns of type images final String orderBy = MediaStore.Images.Media.DATE_TAKEN;//order data by date Cursor imagecursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy + " DESC");//get all data in Cursor by sorting in DESC order galleryImageUrls = new ArrayList<String>();//Init array //Loop to cursor count for (int i = 0; i < imagecursor.getCount(); i++) { imagecursor.moveToPosition(i); int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);//get column index galleryImageUrls.add(imagecursor.getString(dataColumnIndex));//get Image from column index System.out.println("Array path" + galleryImageUrls.get(i)); } } //Set Up GridView method private void setUpGridView() { imagesAdapter = new GridView_Adapter(CustomGallery_Activity.this, galleryImageUrls, true); galleryImagesGridView.setAdapter(imagesAdapter); } //Set Listeners method private void setListeners() { selectImages.setOnClickListener(this); } //Show hide select button if images are selected or deselected public void showSelectButton() { ArrayList<String> selectedItems = imagesAdapter.getCheckedItems(); if (selectedItems.size() > 0) { selectImages.setText(selectedItems.size() + " - Images Selected"); selectImages.setVisibility(View.VISIBLE); } else selectImages.setVisibility(View.GONE); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.selectImagesBtn: //When button is clicked then fill array with selected images ArrayList<String> selectedItems = imagesAdapter.getCheckedItems(); //Send back result to MainActivity with selected images Intent intent = new Intent(); intent.putExtra(MainActivity.CustomGalleryIntentKey, selectedItems.toString());//Convert Array into string to pass data setResult(RESULT_OK, intent);//Set result OK finish();//finish activity break; } } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
package com.selectmultipleimages_demo; import android.content.Context; import android.graphics.Bitmap; import android.util.SparseBooleanArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ImageView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.ArrayList; /** * Created by SONU on 31/10/15. */ public class GridView_Adapter extends BaseAdapter { private Context context; private ArrayList<String> imageUrls; private SparseBooleanArray mSparseBooleanArray;//Variable to store selected Images private DisplayImageOptions options; private boolean isCustomGalleryActivity;//Variable to check if gridview is to setup for Custom Gallery or not public GridView_Adapter(Context context, ArrayList<String> imageUrls, boolean isCustomGalleryActivity) { this.context = context; this.imageUrls = imageUrls; this.isCustomGalleryActivity = isCustomGalleryActivity; mSparseBooleanArray = new SparseBooleanArray(); options = new DisplayImageOptions.Builder() .cacheInMemory(true) .resetViewBeforeLoading(true).cacheOnDisk(true) .considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565) .build(); } //Method to return selected Images public ArrayList<String> getCheckedItems() { ArrayList<String> mTempArry = new ArrayList<String>(); for (int i = 0; i < imageUrls.size(); i++) { if (mSparseBooleanArray.get(i)) { mTempArry.add(imageUrls.get(i)); } } return mTempArry; } @Override public int getCount() { return imageUrls.size(); } @Override public Object getItem(int i) { return imageUrls.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int position, View view, ViewGroup viewGroup) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (view == null) view = inflater.inflate(R.layout.customgridview_item, viewGroup, false);//Inflate layout CheckBox mCheckBox = (CheckBox) view.findViewById(R.id.selectCheckBox); final ImageView imageView = (ImageView) view.findViewById(R.id.galleryImageView); //If Context is MainActivity then hide checkbox if (!isCustomGalleryActivity) mCheckBox.setVisibility(View.GONE); ImageLoader.getInstance().displayImage("file://" + imageUrls.get(position), imageView, options);//Load Images over ImageView mCheckBox.setTag(position);//Set Tag for CheckBox mCheckBox.setChecked(mSparseBooleanArray.get(position)); mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener); return view; } CompoundButton.OnCheckedChangeListener mCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);//Insert selected checkbox value inside boolean array ((CustomGallery_Activity) context).showSelectButton();//call custom gallery activity method } }; } |
11. Now in MainActivity we will get the selected images from Custom Gallery in onActivityResult() method. Set all image array for loading over gridview.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
protected void onActivityResult(int requestcode, int resultcode, Intent imagereturnintent) { super.onActivityResult(requestcode, resultcode, imagereturnintent); switch (requestcode) { case CustomGallerySelectId: if (resultcode == RESULT_OK) { String imagesArray = imagereturnintent.getStringExtra(CustomGalleryIntentKey);//get Intent data //Convert string array into List by splitting by ',' and substring after '[' and before ']' List<String> selectedImages = Arrays.asList(imagesArray.substring(1, imagesArray.length() - 1).split(", ")); } break; } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//Read Shared Images private void getSharedImages() { //If Intent Action equals then proceed if (Intent.ACTION_SEND_MULTIPLE.equals(getIntent().getAction()) && getIntent().hasExtra(Intent.EXTRA_STREAM)) { ArrayList<Parcelable> list = getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);//get Parcelabe list ArrayList<String> selectedImages = new ArrayList<>(); //Loop to all parcelable list for (Parcelable parcel : list) { Uri uri = (Uri) parcel;//get URI String sourcepath = getPath(uri);//Get Path of URI selectedImages.add(sourcepath);//add images to arraylist } loadGridView(selectedImages);//call load gridview } } //get actual path of uri public String getPath(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(uri, projection, null, null, null); startManagingCursor(cursor); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
package com.selectmultipleimages_demo; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Parcelable; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.GridView; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static Button openCustomGallery; private static GridView selectedImageGridView; private static final int CustomGallerySelectId = 1;//Set Intent Id public static final String CustomGalleryIntentKey = "ImageArray";//Set Intent Key Value @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); setListeners(); getSharedImages(); } //Init all views private void initViews() { openCustomGallery = (Button) findViewById(R.id.openCustomGallery); selectedImageGridView = (GridView) findViewById(R.id.selectedImagesGridView); } //set Listeners private void setListeners() { openCustomGallery.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.openCustomGallery: //Start Custom Gallery Activity by passing intent id startActivityForResult(new Intent(MainActivity.this, CustomGallery_Activity.class), CustomGallerySelectId); break; } } protected void onActivityResult(int requestcode, int resultcode, Intent imagereturnintent) { super.onActivityResult(requestcode, resultcode, imagereturnintent); switch (requestcode) { case CustomGallerySelectId: if (resultcode == RESULT_OK) { String imagesArray = imagereturnintent.getStringExtra(CustomGalleryIntentKey);//get Intent data //Convert string array into List by splitting by ',' and substring after '[' and before ']' List<String> selectedImages = Arrays.asList(imagesArray.substring(1, imagesArray.length() - 1).split(", ")); loadGridView(new ArrayList<String>(selectedImages));//call load gridview method by passing converted list into arrayList } break; } } //Load GridView private void loadGridView(ArrayList<String> imagesArray) { GridView_Adapter adapter = new GridView_Adapter(MainActivity.this, imagesArray, false); selectedImageGridView.setAdapter(adapter); } //Read Shared Images private void getSharedImages() { //If Intent Action equals then proceed if (Intent.ACTION_SEND_MULTIPLE.equals(getIntent().getAction()) && getIntent().hasExtra(Intent.EXTRA_STREAM)) { ArrayList<Parcelable> list = getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);//get Parcelabe list ArrayList<String> selectedImages = new ArrayList<>(); //Loop to all parcelable list for (Parcelable parcel : list) { Uri uri = (Uri) parcel;//get URI String sourcepath = getPath(uri);//Get Path of URI selectedImages.add(sourcepath);//add images to arraylist } loadGridView(selectedImages);//call load gridview } } //get actual path of uri public String getPath(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(uri, projection, null, null, null); startManagingCursor(cursor); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } |
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.
1 2 3 4 5 6 7 8 |
<intent-filter> <!-- Intent filter action for receiving multiple images --> <action android:name="android.intent.action.SEND_MULTIPLE" /> <category android:name="android.intent.category.DEFAULT" /> <!-- To receive Images set mime type to image --> <data android:mimeType="image/*" /> </intent-filter> |
And use your MyApplication in application name that we had created in above steps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.selectmultipleimages_demo"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <!-- Intent filter action for receiving multiple images --> <action android:name="android.intent.action.SEND_MULTIPLE" /> <category android:name="android.intent.category.DEFAULT" /> <!-- To receive Images set mime type to image --> <data android:mimeType="image/*" /> </intent-filter> </activity> <!-- Custom Gallery Activity --> <activity android:name=".CustomGallery_Activity" android:label="@string/customGallery" /> </application> </manifest> |
15. Now, you are all done, run your app and you will get the output as shown in video.
Thanks.
Subscribe to us and get the latest news.
46 Comments
Prakhar
Tuesday, December 8th, 2015Hi m not able to add library could you please help
Droid
Friday, December 11th, 2015Hi Prakhar,
What error you are facing can you tell me?
kuldeep
Thursday, April 13th, 2017Hi Friend i need your help.
Dr. Droid
Thursday, April 13th, 2017Hi Kuldeep,
How can i help you?
Thanks
Sur
Wednesday, October 5th, 2016I am facing the same problem as well
Vidit
Friday, February 19th, 2016Thank 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, 2016Hi Vidit,
Please check http://stackoverflow.com/questions/20322528/uploading-images-to-server-android link for image uploading to Server.
Thanks
jafar
Friday, March 11th, 2016thank you so much for tutorial.
but how to display images from a folder assets android studio project too ?
Droid
Friday, March 11th, 2016Hi 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, 2016Thank 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, 2016gridView 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, 2016Hi 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, 2016How to send multiple images to server php from android
Dr. Droid
Saturday, August 27th, 2016Hi Satya,
Below are some links I am sharing with you to how to send multiple images to server.
http://stackoverflow.com/questions/32311484/fastest-way-to-upload-multiple-image-to-server-in-android
http://stackoverflow.com/questions/8940969/uploading-multiple-image-on-php-server-from-android
Thanks
Sur
Wednesday, October 5th, 2016Could 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, 2016Hi 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, 2017when 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, 2017Hi 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, 2017how to send selected multiple images from gridview to display in other activity .
Dr. Droid
Wednesday, July 5th, 2017Hi 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, 2017Could 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, 2017there is an error while adding library
Configuration with name ‘Default’ not found
Dr. Droid
Friday, August 11th, 2017Hi Shivam Goyal,
Please check the links below and do a try :
https://stackoverflow.com/questions/22743582/error-configuration-with-name-default-not-found-in-android-studio
https://stackoverflow.com/questions/17188489/android-studio-gradle-configuration-with-name-default-not-found
Thanks
Stan
Friday, November 10th, 2017Thank 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, 2017Hi 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, 2017Work fine now!
Thank you for your help, Dr Droid!
Have a nice day
Stan
sumanth
Saturday, December 16th, 2017hi,
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, 2017Hi Sumanth,
Please see the below link:
https://stackoverflow.com/questions/28695045/how-does-android-studio-import-gradle-projects
Thanks
sumanth
Tuesday, December 19th, 2017error ::
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, 2017Hi 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, 2017After 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, 2017Hi Sumanth,
Can you share your xml layout code with me at my mail id.
Thanks
Ash
Sunday, February 4th, 2018Hi, 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, 2018Hi 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, 2018Thanks 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, 2018Hi 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, 2018Hello 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, 2018Hi 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, 2018Hello, 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, 2018Hi 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, 2019How can I upload this images to Firebase Storage??
Dr. Droid
Saturday, March 23rd, 2019Hi SM,
For this you have to go through firebase storage documentation.
Thanks
Amine
Thursday, May 9th, 2019hello 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, 2019Hi 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, 2020Hello 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, 2020Hi 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