How to get Bitmap from ImageView in Android

In this android programming code example, we are going to illustrate how to get bitmap from an ImageView in android.

There are times when you might need to convert an image on ImageView to bitmap.

Below is the steps to follow to get bitmap from ImageView in android.

Steps

  1. Add two ImageViews with one source image and Button view to the xml layout file
  2. Get the instances of the Views in the activity class.
  3. Use the method to get the image sourceĀ  to bitmap on button clicked
  4. Display the bitmap to the second ImageView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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"
    android:gravity="center">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:src="@drawable/camera"
        android:layout_margin="10dp"/>

    <ImageView
        android:id="@+id/bitmapImageView"
        android:layout_width="wrap_content"
        android:layout_height="200dp" />

    <Button
        android:layout_marginTop="40dp"
        android:id="@+id/bitmapToImageView"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:text="Convert to Bitmap"
        android:padding="12dp"
        android:textColor="@color/colorWhite"
        android:background="@color/colorAccent"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

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

        final ImageView imageView = findViewById(R.id.imageView2);
        final ImageView bitmapImageView = findViewById(R.id.bitmapImageView);

        Button button = findViewById(R.id.bitmapToImageView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bitmapImageView.setImageBitmap(imageView2Bitmap(imageView));
            }
        });
    }

    private Bitmap imageView2Bitmap(ImageView view){
        Bitmap bitmap = ((BitmapDrawable)view.getDrawable()).getBitmap();
        return bitmap;
    }
}

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment