2012-11-02 41 views
2

我正在使用通用圖像加載程序1.6.2.jar(最新)。 我正在嘗試使用此庫下載並緩存該圖像。我有47個圖像從服務器下載,共5.22 Mb.My最大圖像的尺寸是720X480和143KB大小。它順利,直到40-41影像後,它給錯誤通用圖像加載程序:OutOfMemory錯誤

11-02 16:30:12.150: E/ImageLoader(31033): null 
11-02 16:30:12.150: E/ImageLoader(31033): java.lang.OutOfMemoryError 
11-02 16:30:12.150: E/ImageLoader(31033): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
11-02 16:30:12.150: E/ImageLoader(31033): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493) 
11-02 16:30:12.150: E/ImageLoader(31033): at com.nostra13.universalimageloader.core.ImageDecoder.decode(ImageDecoder.java:83) 
11-02 16:30:12.150: E/ImageLoader(31033): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.saveImageOnDisc(LoadAndDisplayImageTask.java:218) 
11-02 16:30:12.150: E/ImageLoader(31033): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:138) 
11-02 16:30:12.150: E/ImageLoader(31033): at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:72) 
11-02 16:30:12.150: E/ImageLoader(31033): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442) 
11-02 16:30:12.150: E/ImageLoader(31033): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
11-02 16:30:12.150: E/ImageLoader(31033): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
11-02 16:30:12.150: E/ImageLoader(31033): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
11-02 16:30:12.150: E/ImageLoader(31033): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
11-02 16:30:12.150: E/ImageLoader(31033): at java.lang.Thread.run(Thread.java:856) 
11-02 16:30:47.170: E/Adreno200-ES20(31033): <qgl2DrvAPI_glUseProgram:1318>: **** 31033: glUseProgram(3) 
11-02 16:30:47.170: E/Adreno200-ES20(31033): <qgl2DrvAPI_glUseProgram:1318>: **** 31033: glUseProgram(6) 

我所看到的和落實有關git的樞紐OOM建議。我跟着他們,我仍然有錯誤。 這裏是我的代碼

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
     cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"neongall"); 
    else 
     cacheDir=context.getCacheDir(); 
    if(!cacheDir.exists()) 
     cacheDir.mkdirs(); 

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) 
    .threadPoolSize(3) 
    .threadPriority(Thread.NORM_PRIORITY - 1) 
    .memoryCache(new WeakMemoryCache()) 
    .denyCacheImageMultipleSizesInMemory() 
    .offOutOfMemoryHandling() 
    .discCacheExtraOptions(720, 480, CompressFormat.JPEG, 75) 
    .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation 
    .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) 
    .imageDownloader(new URLConnectionImageDownloader(120 * 1000, 120 * 1000)) // connectTimeout (5 s), readTimeout (20 s) 
    .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) 
    .enableLogging() 
    .build(); 
    //Initialize ImageLoader with created configuration. Do it once on Application start. 
    imageLoader.init(config); 


    options = new DisplayImageOptions.Builder() 
    .cacheOnDisc() 
    .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) 
    .displayer(new RoundedBitmapDisplayer(10)) 
    .build(); 

在此先感謝。

回答

15

首先你的配置:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) 
.threadPoolSize(3) // equal to default value 
.threadPriority(Thread.NORM_PRIORITY - 1) // equal to default value 
.memoryCache(new WeakMemoryCache()) 
.denyCacheImageMultipleSizesInMemory() 
.offOutOfMemoryHandling() 
.discCacheExtraOptions(720, 480, CompressFormat.JPEG, 75) 
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation 
.discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // equal to default value 
.imageDownloader(new URLConnectionImageDownloader(120 * 1000, 120 * 1000)) // connectTimeout (5 s), readTimeout (20 s) 
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // equal to default value 
.enableLogging() 
.build(); 

等於

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) 
.memoryCache(new WeakMemoryCache()) 
.denyCacheImageMultipleSizesInMemory() 
.offOutOfMemoryHandling() 
.discCacheExtraOptions(720, 480, CompressFormat.JPEG, 75) 
.discCache(new UnlimitedDiscCache(cacheDir)) 
.imageDownloader(new URLConnectionImageDownloader(120 * 1000, 120 * 1000)) 
.enableLogging() 
.build(); 

禁止複製配置從README!

你爲什麼要關閉OutOfMemory處理?如果你這樣做,那麼你應該自己在ImageLoadingListener.onLoadingFailed(FailReason.OUT_OF_MEMORY)中處理OOM錯誤。

我對你的建議:在配置

    • 刪除.offOutOfMemoryHandling()選項刪除在顯示選項中配置.discCacheExtraOptions(720, 480, CompressFormat.JPEG, 75)選項
    • 使用.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
    • 你真的需要RoundedBitmapDisplayer?請參閱文檔,它在舍入期間在內存中創建額外的位圖。如果可以,請避免使用它。
  • 相關問題