Get Bitmap attached to ImageView
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To get the bitmap currently shown in an ImageView, you first need to know what kind of drawable is attached. If it is a BitmapDrawable, you can read the bitmap directly. If it is another drawable type, such as a vector or color drawable, you need to render it into a new bitmap instead.
The Direct Case: BitmapDrawable
If the image came from setImageBitmap() or from a bitmap-backed resource, this is usually enough:
This is the simplest and cheapest path because no new image needs to be created.
When the Drawable Is Not a Bitmap
An ImageView can hold many drawable types. If you get a vector drawable or another non-bitmap drawable, cast-and-read will fail. In that case, draw the drawable onto a bitmap-backed canvas:
Then:
Watch Out for Display Size Versus Source Size
The bitmap you extract may not match the on-screen size of the ImageView. The drawable can be scaled by:
- '
scaleType' - View bounds
- Density conversions
If you need a bitmap that matches the rendered view size exactly, capture the view's dimensions and draw into a bitmap of that size instead of the drawable's intrinsic size.
That approach captures what the user actually sees.
Memory Matters
Bitmaps can be large. If you only need temporary pixel access, avoid creating multiple copies unnecessarily. Reusing the existing BitmapDrawable bitmap is better than rendering a new bitmap whenever possible.
Also, if the image was loaded by Glide, Picasso, or Coil, the drawable may be managed by that library. In that case, extracting the bitmap repeatedly can create avoidable allocations.
Timing Matters Too
If the ImageView is filled asynchronously, reading the bitmap too early may give you null or a placeholder drawable instead of the final image. In image-loading flows, make sure you read the drawable after the image request has completed and the view has its final content.
Common Pitfalls
- Assuming every
ImageViewdrawable is aBitmapDrawable. - Forgetting that the displayed image size may differ from the original bitmap size.
- Creating extra bitmaps when the existing bitmap is already available.
- Reading the bitmap before the
ImageViewhas been laid out when you actually need the rendered view size.
Summary
- If the drawable is a
BitmapDrawable, get the bitmap directly. - If it is another drawable type, render it into a bitmap with a
Canvas. - Use
imageView.draw(canvas)when you need the exact rendered result. - Pay attention to scaling and memory allocation.
- The right method depends on whether you want the source bitmap or the final on-screen rendering.
That distinction is what makes many "wrong bitmap size" bugs easy to explain after the fact.

