android Lrucache:无法检索缓存的位图

android Lrucache:无法检索缓存的位图,android,bitmap,out-of-memory,android-lru-cache,Android,Bitmap,Out Of Memory,Android Lru Cache,我能够将位图插入LruCache,但无法检索以前解码的图像。我的数据库中有100多个图像。当我对它进行除臭时,我的gc内存在增加。所以我实现了这个功能以重用解码后的图像 protected void onCreate() { LruCache<String, Bitmap> mMemoryCache; codeforLru_oncreate(); aa1=getBitmapFromMemCache(String.valueOf(aa)); if (aa

我能够将位图插入
LruCache
,但无法检索以前解码的图像。我的数据库中有100多个图像。当我对它进行除臭时,我的gc内存在增加。所以我实现了这个功能以重用解码后的图像

protected void onCreate()
{
    LruCache<String, Bitmap> mMemoryCache;
    codeforLru_oncreate();
    aa1=getBitmapFromMemCache(String.valueOf(aa));
    if (aa1 != null) 
    {
      iv.setImageBitmap(aa1);
    } 
    else
    {
      aa1= decodeSampledBitmapFromResource(aa);
      addBitmapToMemoryCache(String.valueOf(aa), aa1);
      iv.setImageBitmap(aa1);
    }
}

你怎么知道它没有返回解码的图像?你使用1/64最大内存的任何原因?检查@kenWolf:的完整示例,因为我在清单文件中使用了android:largeheap=“true”来增加我的堆。它现在变成了256 mb。因此256/64=4 mb的lrucache。@Amulya Khare:在oncreate中,我正在用toast检查,如果(aa1!=null)它永远不会被输入{iv.setImageBitmap(aa1);}
public void codeforLru_oncreate() {

    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    // Use 1/8th of the available memory for this memory cache.
    final int cacheSize = maxMemory / 64;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            //app.setFlagForMemoryAlloc(1);
            return bitmap.getByteCount() / 1024;
        }
    };

}
   // code for adding bitmap into cache memory
  public void addBitmapToMemoryCache(String key, Bitmap bitmap) 
    {
    if (getBitmapFromMemCache(key) == null) {

        mMemoryCache.put(key, bitmap);
    }
}



//code for get bitmap from cache memory
public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}

//decoding here......
public Bitmap decodeSampledBitmapFromResource(byte[] decodethis)
{
     return                BitmapFactory.decodeByteArray(decodethis,0,decodethis.length,option);
}