我能够将位图插入LruCache
但无法检索先前解码的图像。我的数据库中有 100 多张图片。当我对它进行解码时,我的 gc mmemory 正在增加。所以我正在实现它以重用解码后的图像。
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);
}
}
获取最大可用 VM 内存,超过此数量将引发 OutOfMemory 异常。以千字节为单位存储,因为 LruCacheint
在其构造函数中采用了一个。
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);
}