如何在API级别11中获取位图字节数?

时间:2012-06-21 13:42:15

标签: android

自API级别12以来,有一种有用的方法bitmap.getByteCount()但是如何在API 11中获得相同的值?

2 个答案:

答案 0 :(得分:19)

正如dmon所提到的,根据this question bitmap.getByteCount()的评论,这只是一种返回bitmap.getRowBytes() * bitmap.getHeight()的便捷方法。因此,您可以使用自定义方法:

public static long getSizeInBytes(Bitmap bitmap) {
    return bitmap.getRowBytes() * bitmap.getHeight();
}

答案 1 :(得分:6)

最好只使用支持库:

int bitmapByteCount=BitmapCompat.getAllocationByteCount(bitmap)

或者,如果你想自己做的话:

public static int getBitmapByteCount(Bitmap bitmap) {
    if (VERSION.SDK_INT < VERSION_CODES.HONEYCOMB_MR1)
        return bitmap.getRowBytes() * bitmap.getHeight();
    if (VERSION.SDK_INT < VERSION_CODES.KITKAT)
        return bitmap.getByteCount();
    return bitmap.getAllocationByteCount();
}