Get Bitmap attached to ImageView

AndroidImageviewAndroid Bitmap

Android Problem Overview


Given

ImageView image = R.findViewById(R.id.imageView);
image.setImageBitmap(someBitmap);

Is it possible to retrieve the bitmap?

Android Solutions


Solution 1 - Android

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

Solution 2 - Android

This will get you a Bitmap from the ImageView. Though, it is not the same bitmap object that you've set. It is a new one.

imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();

=== EDIT ===

 imageView.setDrawingCacheEnabled(true);
 imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                   MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
 imageView.layout(0, 0, 
                  imageView.getMeasuredWidth(), imageView.getMeasuredHeight()); 
 imageView.buildDrawingCache(true);
 Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
 imageView.setDrawingCacheEnabled(false);

Solution 3 - Android

Write below code

ImageView yourImageView = (ImageView) findViewById(R.id.yourImageView);
Bitmap bitmap = ((BitmapDrawable)yourImageView.getDrawable()).getBitmap();

Solution 4 - Android

For those who are looking for Kotlin solution to get Bitmap from ImageView.

var bitmap = (image.drawable as BitmapDrawable).bitmap

Solution 5 - Android

This code is better.

public static  byte[] getByteArrayFromImageView(ImageView imageView)
    {
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable());
        Bitmap bitmap;
        if(bitmapDrawable==null){
            imageView.buildDrawingCache();
            bitmap = imageView.getDrawingCache();
            imageView.buildDrawingCache(false);
        }else
        {
            bitmap = bitmapDrawable .getBitmap();
        }
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        return stream.toByteArray();
    }

Solution 6 - Android

Other way to get a bitmap of an image is doing this:

Bitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.jellybean_statue);
imageView.setImageBitmap(imagenAndroid);

Solution 7 - Android

try this code:

Bitmap bitmap;
bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlemonView Question on Stackoverflow
Solution 1 - AndroidArslan AnwarView Answer on Stackoverflow
Solution 2 - AndroidSarwar ErfanView Answer on Stackoverflow
Solution 3 - AndroidPankaj TalaviyaView Answer on Stackoverflow
Solution 4 - AndroidAsad Ali ChoudhryView Answer on Stackoverflow
Solution 5 - AndroidAhmad AghazadehView Answer on Stackoverflow
Solution 6 - AndroidHermes TrismegistoView Answer on Stackoverflow
Solution 7 - AndroidDroid_MechanicView Answer on Stackoverflow