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

Dmi*_*sev 3 android

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

Dal*_*mas 19

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

public static long getSizeInBytes(Bitmap bitmap) {
    return bitmap.getRowBytes() * bitmap.getHeight();
}
Run Code Online (Sandbox Code Playgroud)


and*_*per 6

最好只使用支持库:

int bitmapByteCount=BitmapCompat.getAllocationByteCount(bitmap)
Run Code Online (Sandbox Code Playgroud)

或者,如果你想自己做:

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();
}
Run Code Online (Sandbox Code Playgroud)