简体   繁体   English

Android:通用图像加载程序加载大图像

[英]Android: Universal Image Loader Load Large Image

I need to load large image which has 2450 x 2450 pixels dimensions. 我需要加载尺寸为2450 x 2450像素的大图像。

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. 问题是,在低端设备(内存小于1 GB的手机)上,出现内存不足的情况。

This is my DisplayImageOptions 这是我的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. 我尝试根据设备分辨率添加目标大小,但它不起作用,加载的位图仍然具有2450 x 2450像素的尺寸。

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. 我试图将imageScaleType更改为ImageScaleType.EXACTLY ,加载的位图大小调整为1225 x 1225像素,在所有设备上,我需要获得高端设备的原始尺寸,并调整低端设备的尺寸。

The main problem is target size is not working 主要问题是目标尺寸不起作用

Any idea how should I load the image, especially for low end device? 任何想法我应该如何加载图像,特别是对于低端设备?

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. 如果你想自己做这个,我建议用c ++中的NDK来做,因为你有更好的内存管理,可以帮助你使用低端设备。

Simply change imageScaleType to ImageScaleType.EXACTLY_STRETCHED will fix the problem. 只需将imageScaleType更改为ImageScaleType.EXACTLY_STRETCHED解决问题。 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. Stangely UIL将尝试根据目标宽度和目标高度之间的最大值来保持纵横比。

DisplayImageOptions 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM