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