I tried all of the above methods, and they were close, but not quite correct (at least for my situation).
I used bitmap.getByteCount (); inside the sizeOf () method when creating a new LruCache:
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getByteCount(); } };
Then I tried to suggest:
return bitmap.getRowBytes() * bitmap.getHeight();
It was great, but I noticed that the return values ββwere different, and when I used the sentence suggested above, it would not even make a cache on my device. I tested return values ββon Nexus One running on api 3.2 and Galaxy Nexus running on 4.2:
bitmap.getByteCount(); returned-> 15 bitmap.getRowBytes() * bitmap.getHeight(); returned-> 15400
So, to solve my problem, I just did this:
return (bitmap.getRowBytes() * bitmap.getHeight()) / 1000;
instead:
return bitmap.getByteCount();
There may not be the same situation you were in, but it worked for me.
source share