Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

How to Select an Image from Gallery in Android?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Selecting an image from a gallery in Android is required when the user has to upload or set their image as a profile picture or the user wants to send a pic to the other. So in this article, it’s been discussed step by step how to select an image from the gallery and preview the selected image. Have a look at the following image what’s been discussed further in this article.

Select an Image from Gallery in Android

Steps to implement image selection from the gallery

Step 1: Create an empty activity project

Step 2: Working with the activity_main.xml

  • The main layout of the application includes one button to open the image selector, and one Image View to preview the selected image from the gallery.
  • To implement the layout of the application, invoke the following code inside the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
    <!--Button to open the image selector-->
        android:text="SELECT IMAGE"
        android:textSize="18sp" />
    <!--ImageView to preview the selected image-->
        android:layout_marginEnd="16dp" />

 Output UI:

Step 3: Working with the file 

  • In this case, the imageChooser is triggered with the intent of the type “image” and action as ACTION_GET_CONTENT.
  • Invoke the following code to implement the same. Comments are added for better understanding.



import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    // One Button
    Button BSelectImage;
    // One Preview Image
    ImageView IVPreviewImage;
    // constant to compare
      // the activity result code
    int SELECT_PICTURE = 200;
    protected void onCreate(Bundle savedInstanceState) {
        // register the UI widgets with their appropriate IDs
        BSelectImage = findViewById(;
        IVPreviewImage = findViewById(;
        // handle the Choose Image button to trigger
          // the image chooser function
        BSelectImage.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
    // this function is triggered when
      // the Select Image Button is clicked
    void imageChooser() {
        // create an instance of the
          // intent of the type image
        Intent i = new Intent();
        // pass the constant to compare it
          // with the returned requestCode
        startActivityForResult(Intent.createChooser(i, "Select Picture"), SELECT_PICTURE);
    // this function is triggered when user
      // selects the image from the imageChooser
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            // compare the resultCode with the
              // SELECT_PICTURE constant
            if (requestCode == SELECT_PICTURE) {
                // Get the url of the image from data
                Uri selectedImageUri = data.getData();
                if (null != selectedImageUri) {
                    // update the preview image in the layout

 Output: Run on Emulator

Alternative Code: In case: startActivityForResult is deprecated


private void imageChooser()
    Intent i = new Intent();
ActivityResultLauncher<Intent> launchSomeActivity
    = registerForActivityResult(
        new ActivityResultContracts
        result -> {
            if (result.getResultCode()
                == Activity.RESULT_OK) {
                Intent data = result.getData();
                // do your operation from here....
                if (data != null
                    && data.getData() != null) {
                    Uri selectedImageUri = data.getData();
                    Bitmap selectedImageBitmap;
                    try {
                            = MediaStore.Images.Media.getBitmap(
                    catch (IOException e) {


My Personal Notes arrow_drop_up
Last Updated : 17 May, 2022
Like Article
Save Article
Similar Reads
Related Tutorials