Loading Large Bitmaps Efficiently

This lesson teaches you to

Try it out

Note: For most cases, we recommend that you use the Glide
library to fetch, decode, and display bitmaps in your app. Glide abstracts out most of
the complexity in handling these and
other tasks related to working with bitmaps and other images on Android.
For information about using and downloading Glide, visit the
Glide repository on GitHub.

Images come in all shapes and sizes. In many cases they are larger than required for a typical
application user interface (UI). For example, the system Gallery application displays photos taken
using your Android devices's camera which are typically much higher resolution than the screen
density of your device.

Given that you are working with limited memory, ideally you only want to load a lower resolution
version in memory. The lower resolution version should match the size of the UI component that
displays it. An image with a higher resolution does not provide any visible benefit, but still takes
up precious memory and incurs additional performance overhead due to additional on the fly
scaling.

This lesson walks you through decoding large bitmaps without exceeding the per application
memory limit by loading a smaller subsampled version in memory.

Read Bitmap Dimensions and Type

The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources. Choose
the most appropriate decode method based on your image data source. These methods attempt to
allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory
exception. Each type of decode method has additional signatures that let you specify decoding
options via the BitmapFactory.Options class. Setting the inJustDecodeBounds property to true while decoding
avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the
dimensions and type of the image data prior to construction (and memory allocation) of the
bitmap.

To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before
decoding it, unless you absolutely trust the source to provide you with predictably sized image data
that comfortably fits within the available memory.

Load a Scaled Down Version into Memory

Now that the image dimensions are known, they can be used to decide if the full image should be
loaded into memory or if a subsampled version should be loaded instead. Here are some factors to
consider:

Estimated memory usage of loading the full image in memory.

Amount of memory you are willing to commit to loading this image given any other memory
requirements of your application.

Dimensions of the target ImageView or UI component that the image
is to be loaded into.

Screen size and density of the current device.

For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be
displayed in a 128x96 pixel thumbnail in an ImageView.

To tell the decoder to subsample the image, loading a smaller version into memory, set inSampleSize to true in your BitmapFactory.Options object. For example, an image with resolution 2048x1536 that
is decoded with an inSampleSize of 4 produces a
bitmap of approximately 512x384. Loading this into memory uses 0.75MB rather than 12MB for the full
image (assuming a bitmap configuration of ARGB_8888). Here’s
a method to calculate a sample size value that is a power of two based on a target width and
height: