324

Given

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

Is it possible to retrieve the bitmap?

MiguelHincapieC
  • 5,445
  • 7
  • 41
  • 72
lemon
  • 9,155
  • 7
  • 39
  • 47

7 Answers7

826
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105
  • 34
    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();` – Alex Semeniuk Mar 18 '13 at 12:38
  • 2
    This will, occasionally, return a bitmap with some or all black pixels. –  Sep 14 '13 at 13:52
  • 2
    this will not return the original bitmap nor the filtered bitmap if you have applied on the drawable/imageview. – DearDhruv Nov 08 '13 at 05:51
  • Returns null for me. Not sure why. My `ImageView` does not have a drawable in it though, but a photo bitmap. – Azurespot Apr 02 '15 at 05:18
  • @NoniA. - you should open a new question about it – A-S Feb 20 '16 at 20:12
  • 4
    does this work if image in `ImageView` is set from `URI`? `imageView.setImageUri()` – Hendra Anggrian Jun 23 '16 at 09:04
  • 1
    @praneethkumar it does work in my scenario. Thumbs up for this awesome answer! – Hendra Anggrian Jul 04 '16 at 12:05
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);
winklerrr
  • 13,026
  • 8
  • 71
  • 88
Sarwar Erfan
  • 18,034
  • 5
  • 46
  • 57
3

Write below code

ImageView yourImageView = (ImageView) findViewById(R.id.yourImageView);
Bitmap bitmap = ((BitmapDrawable)yourImageView.getDrawable()).getBitmap();
Pankaj Talaviya
  • 3,328
  • 28
  • 31
3

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

var bitmap = (image.drawable as BitmapDrawable).bitmap
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
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();
    }
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
-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);
emmanuel
  • 9,607
  • 10
  • 25
  • 38
-10

try this code:

Bitmap bitmap;
bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
Droid_Mechanic
  • 1,474
  • 14
  • 13