How to get the whole bitmap attached to an ImageView?

24,212

Solution 1

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.

Solution 2

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!

Share:
24,212

Related videos on Youtube

ctsu
Author by

ctsu

Updated on October 28, 2020

Comments

  • ctsu
    ctsu over 3 years

    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?