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 that how we 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 Single Image from Gallery you can check for Java OR Kotlin 

Check For Kotlin

Video:

Step 1: Create a new project OR Open your project

Step 2: Code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!--Picked images will be shown here-->
    <ImageSwitcher
        android:id="@+id/imagesIs"
        android:layout_width="match_parent"
        android:layout_height="400dp"/>
    <!--Show previous image on ImageSwitcher clicking this button-->
    <Button
        android:id="@+id/previousBtn"
        android:text="Previous"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_below="@id/imagesIs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <!--Show Next image on ImageSwitcher clicking this button-->
    <Button
        android:id="@+id/nextBtn"
        android:text="Next"
        android:layout_alignParentEnd="true"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imagesIs"/>
    <!--Pick images clicking this button-->
    <Button
        android:id="@+id/pickImagesBtn"
        android:text="Pick Images"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

MainActivity.java

package com.blogspot.atifsoftwares.imagespickerjava;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    //UI Views
    private ImageSwitcher imagesIs;
    private Button previousBtn, nextBtn, pickImagesBtn;

    //store image uris in this array list
    private ArrayList<Uri> imageUris;

    //request code to pick images
    private static final int PICK_IMAGES_CODE = 0;

    //positoion of selected image
    int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //init UI Views
        imagesIs = findViewById(R.id.imagesIs);
        previousBtn = findViewById(R.id.previousBtn);
        nextBtn = findViewById(R.id.nextBtn);
        pickImagesBtn = findViewById(R.id.pickImagesBtn);

        //init list
        imageUris = new ArrayList<>();

        //setup image switcher
        imagesIs.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(getApplicationContext());
                return imageView;
            }
        });

        //click handle, pick images
        pickImagesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pickImagesIntent();
            }
        });

        //click handle, show previous image
        previousBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position > 0){
                    position--;
                    imagesIs.setImageURI(imageUris.get(position));
                }
                else {
                    Toast.makeText(MainActivity.this, "No Previous images...", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //click handle, show next image
        nextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position < imageUris.size() - 1){
                    position++;
                    imagesIs.setImageURI(imageUris.get(position));
                }
                else {
                    Toast.makeText(MainActivity.this, "No More images...", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void pickImagesIntent(){
        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 Image(s)"), PICK_IMAGES_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGES_CODE){

            if (resultCode == Activity.RESULT_OK){

                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
                    imagesIs.setImageURI(imageUris.get(0));
                    position = 0;
                }
                else {
                    //picked single image
                    Uri imageUri = data.getData();
                    imageUris.add(imageUri);
                    //set image to our image switcher
                    imagesIs.setImageURI(imageUris.get(0));
                    position = 0;
                }

            }

        }
    }
}

Step 3: Run Project




Comments

Popular posts from this blog

Manage External Storage Permission | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Java

Add a Back Button to Action Bar Android Studio (Kotlin)