Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the whole bitmap attached to an ImageView?

I tried to get Bitmap attached to an ImageView, using ImageView.getDrawingCache(); But I found that the returned Bitmap was not the same as I'd like to get from the ImageView. It was always smaller than the real image.

I had known that, the method getDrawingCache() should not have the view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.

Could I get the whole bitmap attached to a ImageView?

like image 666
ctsu Avatar asked Mar 16 '12 17:03

ctsu


2 Answers

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

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

I think that's what you wanted.

like image 67
noob Avatar answered Oct 05 '22 11:10

noob


If your drawble is not always an instanceof BitmapDrawable

Note: ImageView should be set before you do this.

Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
    bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
    Drawable d = mImageView.getDrawable();
    bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    d.draw(canvas);
}

Your bitmap is stored in bitmap.

Voila!

like image 42
mipreamble Avatar answered Oct 05 '22 09:10

mipreamble