Android Question Not able to send file with MQTT

microbox

Active Member
Licensed User
Longtime User
Good day everyone! I would like to send image file(less than 4mb) to other online user. Sending with only text message is no problem.. but when trying to send with file keeps disconnecting. Can you have a moment to look into my the codes below.
B4X:
Public Sub SendMessage(Body As String)
    If connected Then
        client.Publish2("all", CreateMessage(Body), 0, False)
    End If
End Sub


Private Sub CreateMessage(Body As String) As Byte()
    Dim m As Message
    m.Initialize
    m.Body = Body
    Log(m.Body)
    
    If  Main.gvFileName.Length > 1 Then
        If File.Exists(File.DirRootExternal & "/Download" , Main.gvFileName) Then
                Main.gvFolderPath = File.DirRootExternal & "/Download"
                Dim b() As Byte = File.ReadBytes(Main.gvFolderPath,Main.gvFileName)
                m.Body = Body
                m.From = currentName
                m.filename = Main.gvFileName
                m.Image = b
                Log("with file attached")
        End If
        
    Else
            m.Body = Body
            m.From = currentName
            Log("only message")
    
    End If
    Log(">> " & m.Body & " " & m.From & " " & m.filename & " L:" & m.Image.Length)
    Main.gvFileName = ""
    'client.Publish2("all", serializator.ConvertObjectToBytes(m), 0, True)
    Return serializator.ConvertObjectToBytes(m)
End Sub

Please let me know where I'm doing it wrong. Thanks for the help.
 

microbox

Active Member
Licensed User
Longtime User
@Erel I'm still new to this, using jMqttBroker2. There are no errors, but when I try to send with file it terminates the connection. The same when sending small file.
I attached the file I'm working..kindly check if you have time. I appreciate it very much.
 

Attachments

  • SimpleChat.zip
    16.6 KB · Views: 119
Last edited:
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Thanks for the quick response @Erel, I will try to test to send a small file tonight. The code below gives an error.
B4X:
Sub SetMaxMessageSize(broker As MqttBroker, MaxSize As String)
    broker.As(JavaObject).GetFieldJO("config").RunMethod("setProperty", Array("netty.mqtt.message_size", MaxSize))
End Sub
Undeclared variable 'javaobject' is used before it was assigned any value.
 
Upvote 1

microbox

Active Member
Licensed User
Longtime User
Check the JavaObject library and make sure to use B4A v11.
I have updated to B4A v11 but unfortunately when sending file(even small) closes the connection and does not transmit the file.
Below is the print log when it close the connection.
B4X:
Log(">> " & m.Body & " " & m.From & " " & m.filename & " L:" & m.Image.Length)
>> Tom howdy Sam Josie.pdf L:55418
This is the part of the code that sends the message with file converted to bytes
B4X:
If  Main.gvFileName.Length > 1 Then
        If File.Exists(File.DirRootExternal & "/Download" , Main.gvFileName) Then
                Main.gvFolderPath = File.DirRootExternal & "/Download"
                Dim b() As Byte = File.ReadBytes(Main.gvFolderPath,Main.gvFileName)
                m.Body = Body
                m.From = currentName
                m.filename = Main.gvFileName
                m.Image = b
                Log("with file attached")
        End If
 End if
 Return serializator.ConvertObjectToBytes(m)
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
What is the targetsdk in your app?
Are you sure you have access to the file you want to send?
What is the size of the b() Bytearray?

Also you are using two path references.
File.DirRootExternal & "/Download" when using File.Exists

and Main.gvFolderPath when reading the file.
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
What is the targetsdk in your app?
Are you sure you have access to the file you want to send?
What is the size of the b() Bytearray?

Also you are using two path references.
File.DirRootExternal & "/Download" when using File.Exists

and Main.gvFolderPath when reading the file.
Good eve @DonManfred,
Target sdk is 29, b = 55418. I'm pretty sure I got access to the file. But I will test more to check it.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Make sure that Message is declared in the Main module of all relevant apps.
2. Better to rename it to MBMessage or any other more unique name to avoid conflicts.
3. Forget from simply accessing File.DirRootExternal. It is no longer possible.
4. Test with this code:
B4X:
Private Sub CreateMessage(Body As String) As Byte()
    Dim m As Message
    m.Initialize
    m.Body = Body
    m.From = currentName
    m.Filename = Main.gvFileName
    m.Image = Array As Byte(1, 2, 3)
    Return serializator.ConvertObjectToBytes(m)
End Sub
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
1. Make sure that Message is declared in the Main module of all relevant apps.
2. Better to rename it to MBMessage or any other more unique name to avoid conflicts.
3. Forget from simply accessing File.DirRootExternal. It is no longer possible.
4. Test with this code:
B4X:
Private Sub CreateMessage(Body As String) As Byte()
    Dim m As Message
    m.Initialize
    m.Body = Body
    m.From = currentName
    m.Filename = Main.gvFileName
    m.Image = Array As Byte(1, 2, 3)
    Return serializator.ConvertObjectToBytes(m)
End Sub
@Erel thanks I will try it. :)
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Hi @Erel, just a quick question.. sorry but I don't understand this line
B4X:
m.Image = Array As Byte(1, 2, 3)
in my understanding m.image should be in array bytes (converted) from a specified file like below
Dim b() As Byte = File.ReadBytes(Main.gvFolderPath,Main.gvFileName)
m.Image = b
in m.Image = Array As Byte(1, 2, 3) how does it access the file?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
in my understanding m.image should be in array bytes (converted) from a specified file like below
Yes. But the code ONLY creates a Array with 3 Byte. There is no single file involved in this Code.

TEST IT IF THIS WORKS or not.

If this do not work then you don´t need to try to access a file.
Get the short byte array working first.
 
Upvote 1

microbox

Active Member
Licensed User
Longtime User
@DonManfred it works, got it send the file( of 3 bytes).
B4X:
3. Forget from simply accessing File.DirRootExternal. It is no longer possible.
How could I access it?
 
Last edited:
Upvote 0

microbox

Active Member
Licensed User
Longtime User
I moved the file to the application folder and access it, but I still can not send the file. ?
B4X:
Private Sub CreateMessage(Body As String) As Byte()
    Dim m As MessageU
    m.Initialize
    m.Body = Body
    m.From = currentName
    Main.gvFileName = "sample.pdf"
    m.Filename = Main.gvFileName
    Dim DirFile As String
    DirFile = rp2.GetSafeDirDefaultExternal("")
    Log(DirFile)
    Dim b() As Byte = File.ReadBytes(DirFile, Main.gvFileName)
    m.Image = b
    Log(b.Length)

    Return serializator.ConvertObjectToBytes(m)
End Sub
..and it closes the connection.
 
Upvote 0
Top