Resize a large bitmap file to scaled output file on Android
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Resizing large bitmaps on Android is mostly a memory-management problem, not just an image-processing problem. Loading a full-resolution image into memory and scaling it afterward can trigger OutOfMemoryError on older or lower-memory devices. The safe pattern is decode bounds first, choose a sample size, then decode and optionally write a scaled output file.
Decode Dimensions Without Loading the Full Bitmap
Android gives you a way to inspect image size before decoding pixel data. Use inJustDecodeBounds to read metadata only.
This is essential for large images because it avoids allocating the full bitmap just to discover dimensions.
Compute a Reasonable Sample Size
inSampleSize reduces decode size by powers of two. It is the first line of defense against memory blowups.
This does not guarantee exact output size, but it makes decoding much safer.
In other words, inSampleSize is about decoding economically, not about producing the final exact dimensions. Exact sizing usually comes in the next scaling step.
Decode a Smaller Bitmap and Scale Precisely
After choosing sample size, decode the bitmap and then scale to the exact target dimensions if needed.
This two-step process balances memory safety and output control.
Write the Scaled Bitmap to an Output File
Once you have the scaled bitmap, compress and write it to disk.
Compression quality affects file size and visual fidelity. For photos, 85 to 92 is often a practical range.
Keep Aspect Ratio Intentionally
Blindly forcing width and height can distort the image. Often you want to scale to fit within a bounding box while preserving aspect ratio.
Use this before createScaledBitmap when preserving proportions matters.
Avoid UI Thread Work
Large bitmap decode and file write operations should not happen on the main thread. Run them in a background coroutine or worker thread.
Heavy image work on the UI thread causes dropped frames and application-not-responding behavior.
Clean Up Memory When Needed
On modern Android, you generally rely on garbage collection, but large temporary bitmaps still deserve lifecycle attention. Drop references as soon as you are done with them and avoid holding multiple large versions in memory at once.
If the pipeline supports it, decode, scale, write, and release in one narrow scope rather than keeping the bitmap around for unrelated UI work.
That becomes especially important when processing several images in sequence, because one seemingly small leak can multiply into repeated bitmap pressure.
Common Pitfalls
- Decoding the full original image before checking dimensions.
- Setting
inSampleSizetoo late, after the expensive decode already happened. - Forcing a fixed width and height and distorting the aspect ratio.
- Running large decode or compression work on the main thread.
- Writing huge PNG output when JPEG would be smaller and acceptable.
Summary
- Read image bounds first before loading bitmap pixels.
- Use
inSampleSizeto reduce memory pressure during decode. - Scale precisely only after a safe decode step.
- Preserve aspect ratio unless distortion is intentional.
- Write the resized bitmap from a background thread and keep the memory lifetime short.

