Home » Android » java – Get Bitmap from ImageView in Android L

java – Get Bitmap from ImageView in Android L

Posted by: admin April 23, 2020 Leave a comment

Questions:

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");
How to&Answers:

Try this:

imageView.invalidate();
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();

Answer:

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

Answer:

According to this answer, just do it like this:

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

Answer:

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

Answer:

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

Answer:

If Someone Using Glide Library to load image into ImageView Then Try This One.

GlideBitmapDrawable drawable = (GlideBitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();