user2341387
user2341387

Reputation: 303

Android: Universal Image Loader Load Large Image

I need to load large image which has 2450 x 2450 pixels dimensions.

Bitmap bitmap = ImageLoader.getInstance().loadImageSync(url,
        ImageConfig.getImageOptions());

The problem is, on low end device (phone with less than 1 GB RAM), got out of memory exception.

This is my DisplayImageOptions

public static DisplayImageOptions getImageOptions() {
    DisplayImageOptions.Builder options = new DisplayImageOptions.Builder();
    options.delayBeforeLoading(10)
            //The reason I'm using ARGB_8888 because I need to load bitmap without losing it's quality
            .bitmapConfig(Bitmap.Config.ARGB_8888)
            .imageScaleType(ImageScaleType.NONE)
            //I'm not using disk or memory cache
            .cacheOnDisk(false)
            .cacheInMemory(false);
    return options.build();
}

I tried to add target size based on device resolution, but it's not working, loaded bitmap still has 2450 x 2450 pixels dimensions.

int height = orientation == Configuration.ORIENTATION_PORTRAIT ?
        displaymetrics.heightPixels : displaymetrics.widthPixels;

Bitmap bitmap = ImageLoader.getInstance().loadImageSync(imageUri,
        new ImageSize(height, height), ImageConfig.getImageOptions());

I tried to change imageScaleType to ImageScaleType.EXACTLY, loaded bitmap resized to 1225 x 1225 pixels, on all device, I need to get original dimensions for high end device, and resized dimensions for low end device.

The main problem is target size is not working

Any idea how should I load the image, especially for low end device?

Upvotes: 5

Views: 862

Answers (2)

user2341387
user2341387

Reputation: 303

Simply change imageScaleType to ImageScaleType.EXACTLY_STRETCHED will fix the problem. Target size will works as supposed to be. Stangely UIL will try to maintain aspect ratio based on largest value between target width and target height.

DisplayImageOptions

public static DisplayImageOptions getImageOptions() {
    DisplayImageOptions.Builder options = new DisplayImageOptions.Builder();
    options.delayBeforeLoading(10)
        //The reason I'm using ARGB_8888 because I need to load bitmap without losing it's quality
        .bitmapConfig(Bitmap.Config.ARGB_8888)
        .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
        //I'm not using disk or memory cache
        .cacheOnDisk(false)
        .cacheInMemory(false);
    return options.build();
}

Set target size based on device dimensions when loading bitmap

//In this case the device I'm using for testing has 2392 pixels height
int height = orientation == Configuration.ORIENTATION_PORTRAIT ?
        displaymetrics.heightPixels : displaymetrics.widthPixels;
//In this case the device I'm using for testing has 1440 pixels width
int width = orientation == Configuration.ORIENTATION_PORTRAIT ?
        displaymetrics.widthPixels : displaymetrics.heightPixels;

//Loaded bitmap will has 2392 x 2392 pixels dimensions
Bitmap bitmap = ImageLoader.getInstance().loadImageSync(imageUri,
        new ImageSize(width, height), ImageConfig.getImageOptions());

Upvotes: 1

Sergey
Sergey

Reputation: 394

try to use some library like Picasso.

It is really easy to use and it manage resize for you.

If you want to do this by yourself, I suggest to do it with NDK in c++, because there you have a better memory management and can help you with low end devices.

Upvotes: 1

Related Questions