Android development
Bitmap
ImageView
Android programming
Mobile app development

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:

kotlin
1import android.graphics.Bitmap
2import android.graphics.drawable.BitmapDrawable
3import android.widget.ImageView
4
5fun bitmapFromImageView(imageView: ImageView): Bitmap? {
6    val drawable = imageView.drawable
7    return if (drawable is BitmapDrawable) {
8        drawable.bitmap
9    } else {
10        null
11    }
12}

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:

kotlin
1import android.graphics.Bitmap
2import android.graphics.Canvas
3import android.graphics.drawable.Drawable
4
5fun drawableToBitmap(drawable: Drawable): Bitmap {
6    val width = if (drawable.intrinsicWidth > 0) drawable.intrinsicWidth else 1
7    val height = if (drawable.intrinsicHeight > 0) drawable.intrinsicHeight else 1
8
9    val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
10    val canvas = Canvas(bitmap)
11    drawable.setBounds(0, 0, canvas.width, canvas.height)
12    drawable.draw(canvas)
13    return bitmap
14}

Then:

kotlin
1fun safeBitmapFromImageView(imageView: ImageView): Bitmap? {
2    val drawable = imageView.drawable ?: return null
3    return if (drawable is BitmapDrawable) {
4        drawable.bitmap
5    } else {
6        drawableToBitmap(drawable)
7    }
8}

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.

kotlin
1fun renderedBitmapFromImageView(imageView: ImageView): Bitmap {
2    val bitmap = Bitmap.createBitmap(
3        imageView.width,
4        imageView.height,
5        Bitmap.Config.ARGB_8888
6    )
7    val canvas = Canvas(bitmap)
8    imageView.draw(canvas)
9    return bitmap
10}

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 ImageView drawable is a BitmapDrawable.
  • 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 ImageView has 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.


Course illustration
Course illustration

All Rights Reserved.