Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.
Pick Multiple Images from the Gallery – Android Studio - Java
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.usmtip.myapplication; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.net.Uri; 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; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; 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; } } } } }
0 Comments: