Android Question Convert file to send over network with AsyncStreams + B4XSerializator

microbox

Active Member
Licensed User
Longtime User
Hi everyone, I don't understand the conversion of file types like pdf, txt or png to send over the network. The code below I think requires file to be converted to byes before sending.
B4X:
Dim out As OutputStream
out.InitializeToBytesArray(0)
cvs.CreateBitmap.WriteToStream(out, 100, "PNG")
out.Close
mm.Image = out.ToBytesArray
SendData (ser.ConvertObjectToBytes(mm))
I appreciate it if anyone can share a link ( snippet example)or tips how it can be done.

Thanks for taking the time.
joe
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim b() As Byte = File.ReadBytes(...)
'now you have two options:
'send the data directly:
AStream.Write(b)
'or if you want to include more information then use a custom type:
Dim t As YourCustomType = CreateYourCustomType("maybe the file name", b)
AStream.Write(ser.ConvertObjectToBytes(t))

Sub CreateYourCustomType(Name As String, Data() As Byte) As YourCustomType 'this can be generated automatically by hovering over the type declaration
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Is there a way to determine if the file is transferred completely or show a progress bar? The code below is what I use to send.

B4X:
Sub btnSend_Click
    Dim mm As MyMessage
    mm.Initialize
    If IsNumber(txtAge.Text) Then mm.Age = txtAge.Text Else mm.Age = 0
    mm.Name = txtName.Text
    Dim b() As Byte = File.ReadBytes(File.DirAssets, "image.png")
    mm.Image = b
    SendData (ser.ConvertObjectToBytes(mm))
End Sub
 
Upvote 0
Top