choose image from camera and gallery in android
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/chooseImageBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Choose Image" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/takePhotoBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Take Photo" />
</LinearLayout>
MainActivity.java
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.imageView);
AppCompatButton chooseImage = findViewById(R.id.chooseImageBtn);
AppCompatButton takePhoto = findViewById(R.id.takePhotoBtn);
// creating object of ImageChooser
final ImageChooser imageChooser = new ImageChooser(this, getActivityResultRegistry());
// add Observer to activity lifecycle
getLifecycle().addObserver(imageChooser);
chooseImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageChooser.selectImages(new ImagesSelectedListener() {
@Override
public void images(List<Bitmap> bitmaps, List<Uri> uris, int selectedImagesCount) {
if (selectedImagesCount > 0) {
imageView.setImageBitmap(bitmaps.get(0));
}
}
});
}
});
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageChooser.takeImage(new ImagesSelectedListener() {
@Override
public void images(List<Bitmap> bitmaps, List<Uri> uris, int selectedImagesCount) {
if (selectedImagesCount > 0) {
imageView.setImageBitmap(bitmaps.get(0));
}
}
});
}
});
}
}
ImageChooser.java
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.ActivityResultRegistry;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ImageChooser implements DefaultLifecycleObserver {
private final ActivityResultRegistry mRegistry;
private final Context context;
private ActivityResultLauncher<String> chooseImage;
private ActivityResultLauncher<Void> takeImage;
private ImagesSelectedListener imagesSelectedListener;
ImageChooser(Context context, @NonNull ActivityResultRegistry registry) {
this.mRegistry = registry;
this.context = context;
}
public void onCreate(@NonNull LifecycleOwner owner) {
chooseImage = mRegistry.register("chooseImage", owner, new ActivityResultContracts.GetMultipleContents(), new ActivityResultCallback<List<Uri>>() {
@Override
public void onActivityResult(List<Uri> uri) {
final List<Bitmap> bitmaps = new ArrayList<>();
// convert uri to bitmaps
try {
for (int i = 0; i < uri.size(); i++) {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri.get(i));
bitmaps.add(bitmap);
}
imagesSelectedListener.images(bitmaps, uri, uri.size());
} catch (IOException exception) {
exception.printStackTrace();
}
}
});
takeImage = mRegistry.register("takeImage", owner, new ActivityResultContracts.TakePicturePreview(), new ActivityResultCallback<Bitmap>() {
@Override
public void onActivityResult(Bitmap bitmap) {
final List<Bitmap> bitmaps = new ArrayList<>();
bitmaps.add(bitmap);
imagesSelectedListener.images(bitmaps, null, 1);
}
});
}
public void selectImages(ImagesSelectedListener imagesSelectedListener) {
this.imagesSelectedListener = imagesSelectedListener;
chooseImage.launch("image/*");
}
public void takeImage(ImagesSelectedListener imagesSelectedListener) {
this.imagesSelectedListener = imagesSelectedListener;
takeImage.launch(null);
}
}
ImagesSelectedListener.java
import android.graphics.Bitmap;
import android.net.Uri;
import androidx.annotation.Nullable;
import java.util.List;
public interface ImagesSelectedListener {
void images(List<Bitmap> bitmaps, @Nullable List<Uri> uris, int selectedImagesCount);
}
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
Simple Calculator App for Android
How to create Modern Login and Register screen in Android Studio | Material UI Design | Part - 06
Online Tic Tac Toe game Using Firebase Database in Android Studio
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.