我是 android 中 LruCache 的新手,我想在这个缓存上放置和获取位图图像 (JPEG) 以防止内存错误和内存异常,所以我不明白为什么我的代码不起作用。这是我的代码:
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
Bitmap b = BitmapFactory.decodeFile(imagePath);
mMemoryCache.put("mykey", b);
b = mMemoryCache.get("mykey");
imageview.setImageBitmap(b);
这是我的 LruCache 代码:
import android.support.v4.util.LruCache;
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return (bitmap.getRowBytes() * bitmap.getHeight() * 4)/1024;
}
};
}
我不知道为什么不工作:(谢谢