Android Code Snippet GetBitmapSizeinBytes

Gets the size in bytes of a bitmap in a Bitmap Drawable.
Should give an idea to understand better the "in-memory size of the bitmap".

Note: different devices use different color modes, and thus take up different amounts of memory for the full, uncompressed image. The 4 modes listed in the Android SDK are ALPHA_8, ARGB_4444, ARGB_8888, and RGB_565

Requires: JavaObject

B4X:
Sub Whatever
    Dim photo As Bitmap
    Log(GetBitmapSizeinBytes(photo))
End Sub

Sub GetBitmapSizeinBytes (myBitmap As Bitmap) As Int
    Dim ByteCount As Int
    Dim jo As JavaObject = myBitmap
    Dim joSDK As JavaObject
       joSDK.InitializeStatic("android.os.Build.VERSION")
        If joSDK.GetField("SDK_INT") >= 19 Then  'kitkat
            ByteCount = jo.RunMethod("getAllocationByteCount", Null)
        else if joSDK.GetField("SDK_INT") >= 12 Then   'Android 3.1 Honeycomb
            ByteCount = jo.RunMethod("getByteCount", Null)
        Else     'earlier Android versions 
            ByteCount = jo.RunMethod("getRowBytes", Null) * myBitmap.Height

            'the code below could replace the formula above for earlier Android versions
           'ByteCount = myBitmap.Width * myBitmap.Height * 4                                               
        End If
    Return ByteCount
End Sub

Hope it might turn out to be useful for someone.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…