【问题标题】:Android: Universal Image Loader Load Large ImageAndroid:通用图像加载器加载大图像
【发布时间】:2017-04-07 04:39:24
【问题描述】:

我需要加载尺寸为 2450 x 2450 像素的大图像。

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

问题是,在低端设备(RAM 小于 1 GB 的手机)上,出现内存不足异常。

这是我的 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();
}

我尝试根据设备分辨率添加目标大小,但它不起作用,加载的位图仍然具有 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());

我尝试将 imageScaleType 更改为 ImageScaleType.EXACTLY,将加载的位图调整为 1225 x 1225 像素,在所有设备上,我需要获取高端设备的原始尺寸,以及低端设备的调整尺寸。

主要问题是目标大小不起作用

知道我应该如何加载图像,尤其是对于低端设备?

【问题讨论】:

  • 你为什么不使用缓存
  • 因为不需要,即使使用缓存在低端设备上仍然出现内存不足的异常
  • 加载时调整图像大小。相当标准。
  • 这就是我想要做的,但目标大小并不能正常工作

标签: android bitmap universal-image-loader


【解决方案1】:

尝试使用一些库,例如Picasso

它非常易于使用,并且可以为您管理调整大小。

如果你想自己做,我建议用 C++ 中的 NDK 做,因为你有更好的内存管理,可以帮助你处理低端设备。

【讨论】:

    【解决方案2】:

    只需将 imageScaleType 更改为 ImageScaleType.EXACTLY_STRETCHED 即可解决问题。目标大小将按预期工作。有时,UIL 会尝试根据目标宽度和目标高度之间的最大值来保持纵横比。

    显示图像选项

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

    加载位图时根据设备尺寸设置目标尺寸

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

    【讨论】:

      猜你喜欢
      • 2016-06-23
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多