Home > Software engineering >  Is there a way to determine Bitmap.getByteCount based on the image width, height and file size?
Is there a way to determine Bitmap.getByteCount based on the image width, height and file size?

Time:03-19

Based on

https://android.googlesource.com/platform/frameworks/base/ /master/graphics/java/android/graphics/RecordingCanvas.java

/** @hide */
private static int getPanelFrameSize() {
    final int DefaultSize = 100 * 1024 * 1024; // 100 MB;
    return Math.max(SystemProperties.getInt("ro.hwui.max_texture_allocation_size", DefaultSize),
            DefaultSize);
}
/** @hide */
public static final int MAX_BITMAP_SIZE = getPanelFrameSize();

/** @hide */
@Override
protected void throwIfCannotDraw(Bitmap bitmap) {
    super.throwIfCannotDraw(bitmap);
    int bitmapSize = bitmap.getByteCount();
    if (bitmapSize > MAX_BITMAP_SIZE) {
        throw new RuntimeException(
                "Canvas: trying to draw too large("   bitmapSize   "bytes) bitmap.");
    }
}

There is a hard limit 100MB bitmap.getByteCount() imposed on ImageView.

Is there any way we can figure out bitmap.getByteCount(), without perform I/O reading on image file itself.

This will help us to determine how we want to load the image into ImageView, without hitting the hard limit.

Currently, in our DB, we store the following meta data of the image file.

- Image file path
- Image type (PNG or JPG)
- Image file size in byte
- Image width
- Image height

Thanks.

CodePudding user response:

There isn't a really limit for ImageView Memory, but there is a limit for loaded/decoded Bitmap and it depends on Device/hardware. Usually it is "4096 x 4096 pixels x 4 (argb)" = near 70MB, but some device could have 8192x8192x4.

You can use this (pseudo code):

options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filepath, options)
final int H = options.outHeight;
final int W = options.outWidth;

then check its ColorSpace (ARGB or just RGB) and moltiply W x H x 3_or_4 to get Total bytes.

  • Related