Android Question Diffrence size of file in BitmapToString & ImageToBytes & File.ReadBytes

Masoud44

Member
Hi everyone
Why is the file size calculation so different in these three methods?
B4A:
'image8.jpg size 84.5 KB (86,547 bytes)

    Dim bmp As Bitmap
    bmp.Initialize(File.DirAssets, "image8.jpg")
    Log("bmp string length : "&BitmapToString(bmp).Length)                   ' 951116 byte
   
    Dim bb() As Byte=ImageToBytes(bmp)
    Log("bb byte length : "&bb.Length)                                              ' 309169 byte

    Dim buffer() As Byte = File.ReadBytes(File.DirAssets, "image8.jpg")
    Log("buffer byte length : "&buffer.Length)                                   ' 86547 byte
 

Masoud44

Member
Post the code of these methods.
Hi Erel

1- BitmapToString(bmp As B4XBitmap)
BitmapToString(bmp As B4XBitmap):
Private Sub BitmapToString(bmp As B4XBitmap) As String  'XUI
    Dim out As OutputStream
    out.InitializeToBytesArray(1000)
    bmp.WriteToStream(out, 100, "PNG")
    out.Close
    Dim su As StringUtils
    Return su.EncodeBase64(out.ToBytesArray)
End Sub

2- ImageToBytes(Image As Bitmap)
ImageToBytes(Image As Bitmap):
Public Sub ImageToBytes(Image As Bitmap) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    Image.WriteToStream(out, 100, "JPEG")
    out.Close
    Return out.ToBytesArray
End Sub

3- File.ReadBytes(dir As String, fileName As String)
 
Upvote 0

Masoud44

Member
It encodes with base64 which adds about 33% overhead (with the advantage of encoding the raw bytes as a valid string).
Thank you for your explanation.
About BitmapToString i got it but what is difference between ImageToBytes(Image As Bitmap) with File.ReadBytes(dir As String, fileName As String) ?
size returned with ImageToBytes over than size returned with File.ReadBytes
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…