Where the heck is Bitmap getByteCount()?

后端 未结 7 2031
时光取名叫无心
时光取名叫无心 2021-01-02 05:46

I know the Android platform is a huge mess, over complicated and over-engineered, but seriously to get the size of a bitmap, is it really necessary to do all those conversio

7条回答
  •  醉酒成梦
    2021-01-02 06:01

    I tried all of the above methods and they were close, but not quite right (for my situation at least).

    I was using bitmap.getByteCount(); inside of the sizeOf() method when creating a new LruCache:

    mMemoryCache = new LruCache(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getByteCount();
        }
    };  
    

    I then tried the suggested:

    return bitmap.getRowBytes() * bitmap.getHeight();
    

    This was great, but I noticed that the returned values were different and when I used the suggestion above, it would not even make a cache on my device. I tested the return values on a Nexus One running api 3.2 and a Galaxy Nexus running 4.2:

    bitmap.getByteCount(); returned-> 15  
    bitmap.getRowBytes() * bitmap.getHeight(); returned-> 15400
    

    So to solve my issue, I simply did this:

    return (bitmap.getRowBytes() * bitmap.getHeight()) / 1000; 
    

    instead of:

    return bitmap.getByteCount();  
    

    May not be the same situation you were in, but this worked for me.

提交回复
热议问题