Android Code Snippet Resize Bitmap with a maximum Byte limit

Sometimes the images we use in our Apps are overly large. In some cases, images are entered by the user and therefore we do not know the size. And to manage them we need them to fall within a certain Byte limit.

How to reduce them within the limit?

Here is a code that allows you to redimension the images while maintaining the proportion and returning within the Byte limit that serves us

B4X:
Dim B As Bitmap = ResizeBitmapMaxByte(LoadBitmap(File.DirAssets,"test.png"),10240) ' 10 k

' Sub for resize
Sub ResizeBitmapMaxByte(Original As Bitmap, MaxByte As Long) As Bitmap
    Dim ByteSize As Long
    Dim ObjectBitmap As JavaObject = Original
    Dim Api_Lelvel As JavaObject

    Api_Lelvel.InitializeStatic("android.os.Build.VERSION")
    If Api_Lelvel.GetField("SDK_INT") >= 19 Then  'kitkat
        ByteSize = ObjectBitmap.RunMethod("getAllocationByteCount", Null)
    else if Api_Lelvel.GetField("SDK_INT") >= 12 Then   'Android 3.1 Honeycomb
        ByteSize = ObjectBitmap.RunMethod("getByteCount", Null)
    Else     'earlier Android versions
        ByteSize = ObjectBitmap.RunMethod("getRowBytes", Null) * Original.Height
    End If

    Dim Ratio As Float = Sqrt(MaxByte/ByteSize)

    If Ratio<1 Then
        Dim r As Reflector
        Dim b As Bitmap
        Dim Width As Int = (Original.Width * Ratio)
        Dim Height As Int = (Original.Height * Ratio)

        b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
            Array As Object(Original, Width , Height, False), _
            Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
        Return b
    Else
        Return Original
    End If
End Sub
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
You can also use Bitmap.ResizeBitmap or LoadBitmapResize.
Of course, but this code mainly serves to reduce the image based on weight in bytes. The small size is calculated based on this. So Bitmap.ResizeBitmap could replace the second part of the code.

This is the code using BitmapResize
B4X:
Sub ResizeBitmapMaxByte(Original As Bitmap, MaxByte As Long) As Bitmap
    Dim ByteSize As Long
    Dim ObjectBitmap As JavaObject = Original
    Dim Api_Lelvel As JavaObject

    Api_Lelvel.InitializeStatic("android.os.Build.VERSION")
    If Api_Lelvel.GetField("SDK_INT") >= 19 Then  'kitkat
        ByteSize = ObjectBitmap.RunMethod("getAllocationByteCount", Null)
    else if Api_Lelvel.GetField("SDK_INT") >= 12 Then   'Android 3.1 Honeycomb
        ByteSize = ObjectBitmap.RunMethod("getByteCount", Null)
    Else     'earlier Android versions
        ByteSize = ObjectBitmap.RunMethod("getRowBytes", Null) * Original.Height
    End If

    Dim Ratio As Float = Sqrt(MaxByte/ByteSize)

    If Ratio<1 Then
        Dim Width As Int = (Original.Width * Ratio)
        Dim Height As Int = (Original.Height * Ratio)

        Return Original.Bitmap.ResizeBitmap(width, height, true)
    ELse
        Return Original
    End If
End Sub

This code would only work with B4a 7+, the first code also works for previous versions
 
Last edited:

moster67

Expert
Licensed User
Longtime User
Haven't tried above code but I believe the correct method name is Bitmap.Resize() and not Bitmap.ResizeBitmap().
Thus, the return of the bitmap in the last posted snippet above should be:
B4X:
Return Original.Resize(Width, Height, True)
 

Star-Dust

Expert
Licensed User
Longtime User
I don't remember if any method has changed since it was written. But then the code had been tried
 

Lucas Siqueira

Active Member
Licensed User
Longtime User

Can anyone get an example for B4I (IOS)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…