Pick Multiple Images from the Gallery - Android Studio - Java
How to pick multiple images from the Gallery?
There are some scenarios in which we need to pick multiple images from the Gallery. For example, on Facebook, you are familiar with adding multiple images in a single Post. In this tutorial, we will learn how can upload multiple images from the Gallery. You can pick single or as well as multiple images after learning this tutorial. After picking images, we will show those images in ImageSwitcher.
If you want to learn to pick a single Image from the Gallery, you can check for Java OR Kotlin OR Compose
Video:
Step 1: Create a new project OR Open your project
Code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:padding="20dp" tools:context=".MainActivity"> <!--Picked images will be shown here--> <ImageSwitcher android:id="@+id/imagesIs" android:layout_width="match_parent" android:layout_height="400dp" tools:background="#959595" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginVertical="8dp" android:orientation="horizontal"> <!--Show previous image on ImageSwitcher clicking this button--> <com.google.android.material.button.MaterialButton android:id="@+id/previousBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Previous" /> <Space android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <!--Show Next image on ImageSwitcher clicking this button--> <com.google.android.material.button.MaterialButton android:id="@+id/nextBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" /> </LinearLayout> <!--Pick images clicking this button--> <com.google.android.material.button.MaterialButton android:id="@+id/pickImagesBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Pick Images" /> </LinearLayout>
MainActivity.java
package com.technifysoft.myapplication; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import android.widget.ViewSwitcher; import androidx.activity.EdgeToEdge; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.technifysoft.myapplication.databinding.ActivityMainBinding; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; private static final String TAG = "MAIN_TAG"; //store image uris in this array list private ArrayList<Uri> imageUris; //position of selected image int position = 0; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); //init list imageUris = new ArrayList<>(); //setup image switcher binding.imagesIs.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(getApplicationContext()); return imageView; } }); //click handle, pick images binding.pickImagesBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { pickImageGallery(); } }); //click handle, show previous image binding.previousBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (position > 0) { position--; binding.imagesIs.setImageURI(imageUris.get(position)); } else { Toast.makeText(MainActivity.this, "No Previous images...", Toast.LENGTH_SHORT).show(); } } }); //click handle, show next image binding.nextBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (position < imageUris.size() - 1) { position++; binding.imagesIs.setImageURI(imageUris.get(position)); } else { Toast.makeText(MainActivity.this, "No More images...", Toast.LENGTH_SHORT).show(); } } }); } private void pickImageGallery() { Log.d(TAG, "pickImageGallery: "); //Intent to launch Images Picker e.g. Gallery Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); galleryActivityResultLauncher.launch(intent); } private final ActivityResultLauncher<Intent> galleryActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { //Check if image is picked or not if (result.getResultCode() == RESULT_OK) { //Image Picked from Gallery, get data/intent from ActivityResult Intent data = result.getData(); if (data.getClipData() != null) { //picked multiple images int cout = data.getClipData().getItemCount(); //number of picked images for (int i = 0; i < cout; i++) { //get image uri at specific index Uri imageUri = data.getClipData().getItemAt(i).getUri(); imageUris.add(imageUri); //add to list } //set first image to our image switcher binding.imagesIs.setImageURI(imageUris.get(0)); position = 0; } else { //picked single image Uri imageUri = data.getData(); imageUris.add(imageUri); //set image to our image switcher binding.imagesIs.setImageURI(imageUris.get(0)); position = 0; } } else { //Cancelled Log.d(TAG, "onActivityResult: Cancelled..."); Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show(); } }); }


Comments
Post a Comment