我能够将位图插入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);
}
}
获取最大可用VM内存,超过此数量将引发OutOfMemory异常。存储为千字节,因为LruCache在其构造函数中使用int
。
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);
}