323

Given

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

Is it possible to retrieve the bitmap?

1
  • 1
    yes it, possible when u click on image we will get that if you want this requirement let me know. Commented Nov 29, 2011 at 6:12

7 Answers 7

826
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); 
Sign up to request clarification or add additional context in comments.

7 Comments

Be carefull to check if your image.getDrawable() can actually be cast to BitmapDrawable (to avoid IllegalCastExceptions). If, for instance, you use layers in your image then this snippet will be slightly different: Bitmap bitmap = ((BitmapDrawable)((LayerDrawable)image.getDrawable()).getDrawable(0)).getBitmap();
This will, occasionally, return a bitmap with some or all black pixels.
this will not return the original bitmap nor the filtered bitmap if you have applied on the drawable/imageview.
does this work if image in ImageView is set from URI? imageView.setImageUri()
@praneethkumar it does work in my scenario. Thumbs up for this awesome answer!
|
46

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); 

4 Comments

when it "does not work", what happens? Does it return null or throw exception or what?
it returns null. sometimes I have to reload the page for it to actually appear.
Gives me a null pointer. :( On this line: Bitmap bmap = Bitmap.createBitmap(mImageView.getDrawingCache());
drawingCache is deprecated in Kotlin
3

Write below code

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

1 Comment

i get AppCompatImageView cannot be cast to android.graphics.drawable.BitmapDrawable
3

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

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

1 Comment

i get AppCompatImageView cannot be cast to android.graphics.drawable.BitmapDrawable
0

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(); } 

2 Comments

is it imageView.getDrawable(); -> means getting the image from drawable folder? CMIIW.... @Ahmad
No. You can use this code.Bitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.jellybean_statue);
-4

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

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

Comments

-10

try this code:

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

2 Comments

Could you describe the enhancement over @Arslan 's accepted answer?
you'd better explain why your answer solves his problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.