ImageLoaderLibrary Load Large Images Efficiently

ImageLoaderLibrary Load Large Images Efficiently


This is an Android library used for loading bitmaps efficiently from local disk, If you have big images on disk and want to load it in an ImageView, then you could use this library to load a smaller resolution bitmap from this image. You could use this library to avoid the OutOfMemoryError Exception that occurs when you load high resolution images.
If you have an image with resolution 2048×1536, this would allocate about 12MB in memory to load a full image, you could use this library to load a smaller version of this image with a predefined height and width. This library will load a smaller resolution bitmap version for you. The library will not destroy the aspect ratio of the image.

This library will load the image in its original orientation also by reading the ExifInterface of this image and rotate it appropriately.

The source code of the library is available over github

When to use the ImagesLoaderLibrary

ImagesLoaderLibrary is helpful when you are trying to load Bitmaps of high resolution images from your local disk in Android. Lets say you need to load an image that would take more than 20MB in memory and that image your are loading is saved on disk in a rotated format (some images are written on disk in a rotated format, and you need to read the Exif attributes to display the image in its original orientation), In this case ImagesLoaderLibrary will load it efficiently and in its original orientation. Aspect ratio will be the same as the original image.

How to use ImagesLoaderLibrary

    1. Download
    2. Add this class to your project
    3. Call LoadImage method from ImagesLoaderLibrary class like the following example:

[code language=”java”]
ImageView mImageView = findViewById(;
Bitmap mBitmap = ImageLoaderLibrary.loadImage(“someImagePath”, 512, 512);

  1. Set the following parameters
    • imagePath: the image path you want to load from disk.
    • reqWidth: required width for the lower resolution bitmap to load.
    • reqHeight: required height for the lower resolution bitmap to load.

Note: reqWidth and reqHeight should be as small as possible to be able to load a lower resolution image that could fit in memory.

API Reference

[code language=”java”]
Class ImageLoaderLibrary{
* Use this method to load subsampled bitmap of the image specified by the imagePath. The result bitmap will have the
* original aspect ratio and the original orientation of the image.
* @param imagePath the path of image to be loaded
* @param reqWidth required width of the subsampled version
* @param reqHeight required height of the subsampled version
* @return Bitmap returns the subsampled bitmap of the image
public static Bitmap loadImage(String imagePath, int reqWidth, int reqHeight);

Code Guide

This code is written following  Google Android developers guide

Reading Image Height & Width

Firstly, Image size should be read by using the options you could send to decodeFile method of BitmapFactory class in Android SDK, this could be done by setting inJustDecodeBounds property for true.

[code language=”java”]
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;

Calculate Scale Factor

Scale factor is calculated based on which dimension should be reduced more (height or width). This will create an efficient  smaller resolution version of the image while keeping the aspect ratio fixed.

[code language=”java”]
float scaleFactor;
scaleFactor = Math.max(photoW / imageViewWidth, photoH/ imageViewHeight);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = (int) Math.ceil(scaleFactor);

Get Correct Orientation

Images saved on disk have Exif properties that needs to be processed to load images in its correct orientation.

[code language=”java”]
ExifInterface exif = new ExifInterface(imagePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Matrix matrix = new Matrix();
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {


The source code of the library is available over github

About The Author


No Comments

Leave a Reply