2013-03-20 18 views
5

Tôi có một ứng dụng tải rất nhiều hình ảnh lớn từ xa. Khi tôi sử dụng Bộ tải hình ảnh đa năng của nostra (https://github.com/nostra13/Android-Universal-Image-Loader) Tôi thường gặp lỗi Out Of Memory. Tôi không biết làm thế nào tôi nên thiết lập bộ nạp hình để ngăn chặn điều này.Trình tải ảnh toàn cảnh của Android - làm cách nào để đặt thông số kỹ thuật chính xác?

Đây là hiện thông số kỹ thuật ImageLoader tôi:

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

Và vâng, tôi vượt qua imgDispOpts khi gọi displayImage().

Trả lời

0

Hãy thử cấu hình dưới đây

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

Bạn cũng có thể sử dụng Lazy tải các hình ảnh. https://github.com/thest1/LazyList.

Trình tải ảnh phổ biến dựa trên dự án LazyList của Fedor Vlasov. Phiên bản LazyList được cải thiện.

+0

là những gì công việc của dòng này, discCacheExtraOptions (maxImageWidthForDiscCache, maxImageHeightForDiscCache, CompressFormat.PNG, 0); –

+0

cho bộ nhớ đệm của bitmap với chiều cao và chiều rộng thích hợp thiết lập chiều cao và chiều rộng tối đa – Raghunandan

+0

Có thể ảnh hưởng đến chất lượng hình ảnh nếu chúng tôi đặt chiều cao và chiều rộng tĩnh không? –

5

Thử không lưu bộ nhớ cache trong bộ nhớ, sử dụng loại EXACTLY loại quy mô và RGB_565 để giải mã bitmap.

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

this.imgDispOpts = new DisplayImageOptions.Builder() 
    .cacheOnDisc() 
    .imageScaleType(ImageScaleType.EXACTLY) 
    .bitmapConfig(Bitmap.Config.RGB_565) 
    .build(); 
1

Tôi đã cố gắng giảm kích thước bitmap trong bộ giải mã bitmap, đã làm việc cho tôi.

Trong gói com.nostra13.universalimageloader.core.decode, BaseImageDecoder.java mở,

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; 
} 
+0

Nó hoạt động. Cảm ơn rất nhiều! Hết bộ nhớ ngoại lệ đã khiến tôi phát điên. – NightFury

0

Bạn có thể thử này, nó làm việc tốt cho tôi:

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()); 
Các vấn đề liên quan