How to set a bitmap from resource
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Loading an image from res/drawable into a Bitmap is straightforward in Android, but the correct approach depends on what you plan to do with that image. If you only want to display it, an ImageView can often use the drawable directly. If you need pixel access, resizing, or custom drawing, decoding a bitmap from resources is the right tool.
Decode a Bitmap from Resources
The basic API is BitmapFactory.decodeResource(). It takes a Resources instance and the drawable resource ID, then returns a Bitmap.
That is enough for small images and quick prototypes. The problem is that decodeResource() loads the full image into memory. If the source image is large, that can waste memory or trigger OutOfMemoryError on older or lower-end devices.
Downsample Large Images
If the source asset is much larger than the target view, decode a smaller version. Android lets you inspect image bounds without allocating pixel memory, then choose an inSampleSize.
This pattern is useful when you are decoding photos for thumbnails, cards, or full-screen previews. You still get a Bitmap, but now it is closer to the size the UI actually needs.
Choose the Right API for the Job
There are three common cases:
- Display only: call
imageView.setImageResource(R.drawable.sample_photo)and skip manual bitmap work. - Display with transformation: decode a bitmap, resize or crop it, then set it on the
ImageView. - Pixel-level processing: decode a mutable bitmap and draw on a
Canvasor inspect individual pixels.
If you need a mutable bitmap for drawing, make a copy in a writable config:
That lets you draw overlays, watermarks, or annotations:
Resource Placement and Format
Put raster images in res/drawable, drawable-hdpi, drawable-xhdpi, and similar density-aware folders when appropriate. Android picks the closest resource for the current screen density, which improves sharpness and reduces unnecessary scaling.
For icons and simple illustrations, vector drawables are often better than bitmaps because they scale cleanly and usually consume less space. Use bitmaps when the source is photographic or when your code needs raw pixels.
Common Pitfalls
The biggest mistake is decoding a huge resource at full size just to show it in a small view. That wastes memory and can make scrolling or screen transitions stutter. Use BitmapFactory.Options and sample the image down.
Another issue is decoding on the main thread when the image is large. For simple resources this is fine, but large decodes should happen off the UI thread or through an image-loading library.
Developers also sometimes use Bitmap when a drawable would do. If all you need is display, setImageResource() is simpler and gives the framework more flexibility.
Finally, be careful when holding long-lived bitmap references in activities, adapters, or singletons. Large images can keep memory alive long after the screen should have released it.
Summary
- Use
BitmapFactory.decodeResource()when you need a realBitmapfrom an Android resource. - For plain display,
setImageResource()is often the simpler choice. - Large images should be downsampled with
BitmapFactory.Optionsto avoid memory waste. - Mutable bitmaps are useful when you need to draw or edit pixels.
- Choose bitmaps for photos and pixel work, and vector drawables for scalable artwork.

