【问题标题】:Android Universal Image Loader - how do I set the specs correctly?Android Universal Image Loader - 如何正确设置规格?
【发布时间】:2013-03-09 13:55:23
【问题描述】:

我有一个可以远程加载大量大图像的应用。 当我使用 nostra 的 Universal Image Loader (https://github.com/nostra13/Android-Universal-Image-Loader) 时,我经常会遇到 Out Of Memory 错误。我不知道应该如何设置图像加载器来防止这种情况发生。

这是我当前的 ImageLoader 规格:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .enableLogging()
        .memoryCache(new WeakMemoryCache())
        .build(); 

this.imgDispOpts = new DisplayImageOptions.Builder()
        .cacheInMemory() 
        .cacheOnDisc() 
        .imageScaleType(ImageScaleType.IN_SAMPLE_INT) 
        .build();

this.imageLoader = ImageLoader.getInstance();
this.imageLoader.init(config);

是的,我在调用 displayImage() 时传递了 imgDispOpts。

【问题讨论】:

    标签: android out-of-memory universal-image-loader


    【解决方案1】:

    尽量不要缓存在内存中,使用EXACTLY缩放类型和RGB_565进行位图解码。

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .enableLogging()
        .build(); 
    
    this.imgDispOpts = new DisplayImageOptions.Builder() 
        .cacheOnDisc() 
        .imageScaleType(ImageScaleType.EXACTLY) 
        .bitmapConfig(Bitmap.Config.RGB_565)
        .build();
    

    【讨论】:

      【解决方案2】:

      我尝试减小位图解码器中的位图大小,这对我有用。

      在com.nostra13.universalimageloader.core.decode包中,打开BaseImageDecoder.java,

      public Bitmap decode(ImageDecodingInfo decodingInfo) throws IOException {
              Bitmap decodedBitmap;
              ImageFileInfo imageInfo;
      
              InputStream imageStream = getImageStream(decodingInfo);
              try {
                  imageInfo = defineImageSizeAndRotation(imageStream, decodingInfo);
                  imageStream = resetStream(imageStream, decodingInfo);
                  Options decodingOptions = prepareDecodingOptions(imageInfo.imageSize, decodingInfo);
      
      // add in the decodingOptions here:
      
      decodingOptions.inSampleSize = 10; // or any number
      
      // or you can calculate the resample rate by using the screen size / grid size and the image size
      
      decodedBitmap = BitmapFactory.decodeStream(imageStream, null, decodingOptions);
          } finally {
              IoUtils.closeSilently(imageStream);
          }
          if (decodedBitmap == null) {
              L.e(ERROR_CANT_DECODE_IMAGE, decodingInfo.getImageKey());
          } else {
              decodedBitmap = considerExactScaleAndOrientaiton(decodedBitmap, decodingInfo, imageInfo.exif.rotation, imageInfo.exif.flipHorizontal);
          }
          return decodedBitmap;
      }
      

      【讨论】:

      • 成功了。非常感谢!内存不足异常让我发疯。
      【解决方案3】:

      试试下面的配置

      ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
          .enableLogging()
           discCacheExtraOptions(maxImageWidthForDiscCache, maxImageHeightForDiscCache, CompressFormat.PNG, 0);
          .memoryCache(new WeakMemoryCache())
          .build();
      
       this.imgDispOpts = new DisplayImageOptions.Builder()
          .cacheInMemory() 
          .cacheOnDisc() 
          .imageScaleType(ImageScaleType.EXACTLY) 
          .build();
      

      您还可以使用延迟加载图像。https://github.com/thest1/LazyList。

      基于 Fedor Vlasov 的项目 LazyList 的通用图像加载器。 LazyList 的改进版本。

      【讨论】:

      • 这一行的作用是什么,discCacheExtraOptions(maxImageWidthForDiscCache, maxImageHeightForDiscCache, CompressFormat.PNG, 0);
      • 用于缓存具有适当高度和宽度的位图,设置最大高度和宽度
      • 设置静态高宽对画质有影响吗?
      • 将图像高度和宽度设置为您想要的值,以减少内存不足错误。如果图像太大而无法放入内存,您将遇到 oom 错误
      【解决方案4】:

      你可以试试这个,它对我很有效:

      ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context)
      //Add these lines to your ImageLoader configuration
              .threadPriority(Thread.NORM_PRIORITY - 2);
              .denyCacheImageMultipleSizesInMemory();
              .diskCacheExtraOptions(150, 150, null); // You may change image resolution to fit your needs
      
      
      ImageLoader.getInstance().init(config.build());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-09
        • 1970-01-01
        • 2012-11-01
        • 2012-11-08
        • 2013-06-14
        • 2017-09-08
        • 2021-12-26
        • 2013-10-30
        相关资源
        最近更新 更多