当我尝试使用它们的键检索它们时,我有以下代码来缓存位图。他们总是返回 null 。请帮我 。谢谢你
final int memClass = ((ActivityManager) mcontext.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
final int cacheSize = 1024 * 1024 *memClass ;
Toast.makeText(mcontext,"Max Cache "+cacheSize,Toast.LENGTH_LONG).show();
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.
return bitmap.getByteCount() / 1024;
}
};
获取和设置缓存的函数
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
Toast.makeText(mcontext,"CEO "+key+" - "+bitmap,Toast.LENGTH_LONG).show();
mMemoryCache.put(key, bitmap);
}
else
{
Toast.makeText(mcontext,"NULL",Toast.LENGTH_LONG).show();
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
- - - - - -更新 - - - - - - - - - - - - - - - - - - - ---好吧,经过一些调试发现它正在插入值,但是在我退出应用程序后我无法访问它。当我尝试重新启动应用程序时,缓存会丢失。请有人可以帮助我如何保留该价值。谢谢你