53

I want to get Bitmap from ImageView. I have used following code, but getDrawable() returns null. How to get whole Bitmap from ImageView.

Bitmap bitmap;
if (mImageViewer.getDrawable() instanceof BitmapDrawable) {
    bitmap = ((BitmapDrawable) mImageViewer.getDrawable()).getBitmap();
} else {
    Drawable d = mImageViewer.getDrawable();
    bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    d.draw(canvas);
}
storeImage(bitmap,"final.jpeg");
Bugs Happen
  • 1,938
  • 4
  • 27
  • 54
Utkarsh Srivastav
  • 2,905
  • 7
  • 33
  • 53
  • 2
    Your code should work, make sure your image view has a drawable, if you're setting the image using setBackground use setBitmap instead – elmorabea Nov 26 '14 at 11:42

6 Answers6

86

Try this:

imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Bipin Bharti
  • 978
  • 2
  • 12
  • 26
Pankaj Arora
  • 11,041
  • 2
  • 38
  • 62
33

If you just want the Bitmap from a ImageView the following code may work for you:-

Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();

Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)

Could be that the emulator or device your using has a different density and is trying to pull images from another folder.

If you are using an extension in your image other than .png, .jpg, or .gif, It might not recognize other extension types. http://developer.android.com/guide/topics/resources/drawable-resource.html

droidev
  • 7,104
  • 11
  • 60
  • 93
20

According to this answer, just do it like this:

imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
Community
  • 1
  • 1
ToYonos
  • 15,591
  • 2
  • 48
  • 65
  • 2
    this doesn't work perfectly all the time, usually give Canvas: trying to use recycled bitmap – Arsalan Saleem Feb 21 '15 at 08:08
  • This solution along with this answer(http://stackoverflow.com/questions/5566758/bitmap-from-textview-getdrawingcache-always-null) worked for me: imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight()); – Roy Jun 01 '15 at 12:58
  • getDrawingCache() is depricated – Majid Sadeghi Sep 08 '20 at 10:54
7

For Kotlin:

simply write this code to get the bitmap from an ImageView

imageview.invalidate()
val drawable = imageview.drawable
val bitmap = drawable.toBitmap()
Ghayas
  • 746
  • 6
  • 13
6

If you are trying to get bitmap from Glide loaded image then this will help you

 Drawable dr = ((ImageView) imView).getDrawable();
        Bitmap bmp =  ((GlideBitmapDrawable)dr.getCurrent()).getBitmap();
Ness Tyagi
  • 1,920
  • 22
  • 18
1

Take a picture of the ImagView and convert it to a string to send to the server

    ImageView   ivImage1 = (ImageView ) findViewById(R.id.img_add1_send );
    getStringImage( ( ( BitmapDrawable ) ivImage1.getDrawable( ) ).getBitmap( ) ),



public String getStringImage(Bitmap bm){
    ByteArrayOutputStream ba=new ByteArrayOutputStream(  );
    bm.compress( Bitmap.CompressFormat.PNG,90,ba );
    byte[] by=ba.toByteArray();
    String encod= Base64.encodeToString( by,Base64.DEFAULT );
    return encod;
}
madlymad
  • 6,081
  • 6
  • 34
  • 63
younes
  • 692
  • 7
  • 8
  • sorry out of topic. but how does server (php) receive this string...? is there any encoder steps required? – gumuruh May 29 '20 at 02:38