Android Question How to compress images in ImageView1 to below 600KB?

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Private Sub ResizeImageBasedOnMaxFileSize (Img As B4XBitmap, MaxSize As Long, OutputFolder As String, OutputFile As String)
    Dim Quality As Int = 100
    Dim Size As Long = MaxSize + 1
    Do While Size > MaxSize And Quality >= 10
        Dim out As OutputStream = File.OpenOutput(OutputFolder, OutputFile, False)
        Img.WriteToStream(out, Quality, "JPEG")
        out.Close
        Size = File.Size(OutputFolder, OutputFile)
        Log($"Quality: ${Quality}%, Size: $1.0{Size / 1024}kb"$)
        Quality = Quality - 5
    Loop
End Sub

Usage example:
B4X:
ResizeImageBasedOnMaxFileSize(xui.LoadBitmap(File.DirAssets, "2.jpg"), 600 * 1024, xui.DefaultFolder, "2.jpg")
 
Upvote 0
Top