Android Question whatsapp - attach a file to message

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
hi,

I'm using this code to send WhatsApp messages

B4X:
        Intent1.Initialize(Intent1.ACTION_VIEW, $"https://api.whatsapp.com/send?phone=${EditTextNumber.Text}&text=${EditTextMessange.Text}"$)
        Intent1.SetComponent("android/com.android.internal.app.ResolverActivity")
        StartActivity(Intent1)

how can i add a file?

and - how do i send using WhatsApp bossiness instead ?

thanks
 

Lello1964

Well-Known Member
Licensed User
Longtime User
Invio documento:
    Dim FileName As String = "documento.pdf"  'change it
    File.Copy(shared, FileName, Starter.Provider.SharedFolder, FileName)
    'File.Copy(shared, "documento.pdf", Starter.Provider.SharedFolder, "documento.pdf")
'    If Not (File.Exists(shared,"documento.pdf")) Then
'        File.Copy(File.DirAssets,"documento.pdf",shared,"documento.pdf")
'    End If


    Dim sendIntent As Intent
    sendIntent.Initialize(sendIntent.ACTION_MAIN,"")
    'sendIntent.PutExtra("jid","+393291876000" & "@s.whatsapp.net")
    sendIntent.Action=sendIntent.ACTION_SEND
    sendIntent.SetPackage("com.whatsapp")
    sendIntent.SetComponent("android/com.android.internal.app.ResolverActivity")
    ''sendIntent.SetComponent("com.whatsapp/.ContactPicker")
    sendIntent.PutExtra("android.intent.extra.STREAM", Starter.Provider.GetFileUri("documento.pdf"))
    sendIntent.PutExtra("android.intent.extra.TEXT","Si allega documento :")
    sendIntent.SetType("application/pdf")
    StartActivity(sendIntent)
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Invio documento:
    Dim FileName As String = "documento.pdf"  'change it
    File.Copy(shared, FileName, Starter.Provider.SharedFolder, FileName)
    'File.Copy(shared, "documento.pdf", Starter.Provider.SharedFolder, "documento.pdf")
'    If Not (File.Exists(shared,"documento.pdf")) Then
'        File.Copy(File.DirAssets,"documento.pdf",shared,"documento.pdf")
'    End If


    Dim sendIntent As Intent
    sendIntent.Initialize(sendIntent.ACTION_MAIN,"")
    'sendIntent.PutExtra("jid","+393291876000" & "@s.whatsapp.net")
    sendIntent.Action=sendIntent.ACTION_SEND
    sendIntent.SetPackage("com.whatsapp")
    sendIntent.SetComponent("android/com.android.internal.app.ResolverActivity")
    ''sendIntent.SetComponent("com.whatsapp/.ContactPicker")
    sendIntent.PutExtra("android.intent.extra.STREAM", Starter.Provider.GetFileUri("documento.pdf"))
    sendIntent.PutExtra("android.intent.extra.TEXT","Si allega documento :")
    sendIntent.SetType("application/pdf")
    StartActivity(sendIntent)
Thank you so much
I will try it and revert
Once again thank you and have a great weekend
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Invio documento:
    Dim FileName As String = "documento.pdf"  'change it
    File.Copy(shared, FileName, Starter.Provider.SharedFolder, FileName)
    'File.Copy(shared, "documento.pdf", Starter.Provider.SharedFolder, "documento.pdf")
'    If Not (File.Exists(shared,"documento.pdf")) Then
'        File.Copy(File.DirAssets,"documento.pdf",shared,"documento.pdf")
'    End If


    Dim sendIntent As Intent
    sendIntent.Initialize(sendIntent.ACTION_MAIN,"")
    'sendIntent.PutExtra("jid","+393291876000" & "@s.whatsapp.net")
    sendIntent.Action=sendIntent.ACTION_SEND
    sendIntent.SetPackage("com.whatsapp")
    sendIntent.SetComponent("android/com.android.internal.app.ResolverActivity")
    ''sendIntent.SetComponent("com.whatsapp/.ContactPicker")
    sendIntent.PutExtra("android.intent.extra.STREAM", Starter.Provider.GetFileUri("documento.pdf"))
    sendIntent.PutExtra("android.intent.extra.TEXT","Si allega documento :")
    sendIntent.SetType("application/pdf")
    StartActivity(sendIntent)
hi

i tried, something went wrong
i made some lite changes as i got errors on shared etc (maybe declarations)
so i put it in a sub

B4X:
Sub Send_WhatsAPP_Attachment(Phone As String, Msg As String, FileName As String)
    Dim sendIntent As Intent

    Try
     
        If File.Exists(File.DirInternal,FileName) Then
            File.Delete(File.DirInternal,FileName)
        End If

        File.Copy(File.DirAssets,FileName, File.DirInternal, FileName)
 
        sendIntent.Initialize(sendIntent.ACTION_MAIN,"")
        sendIntent.PutExtra("jid",Phone & "@s.whatsapp.net")
        sendIntent.Action=sendIntent.ACTION_SEND
        sendIntent.SetPackage("com.whatsapp")
        sendIntent.SetComponent("android/com.android.internal.app.ResolverActivity")
        sendIntent.PutExtra("android.intent.extra.TEXT",Msg)
        sendIntent.PutExtra("android.intent.extra.STREAM", File.Combine(File.DirInternal,FileName))
        sendIntent.SetType("application/pdf")

        StartActivity(sendIntent)

    Catch

        Log(LastException.message)

    End Try
 
End Sub

now no errors but when i try to send i get a message that it cannot share

i put a pdf file in assets so i cant try
but what if i want to use another type like jpg ?

and last thing: what should i do if i want to use whatsapp for bussiness too?
what should i change?

any ideas?
hat went wrong?

thanks
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
Hi, I use it with PDF and it works, but I saw that you changed the code, you removed the comments.

As for Wu, I don't have the business version, I can't test.
B4X:
Dim FileName As String = "documento.pdf"  'change it
Sub Send_WhatsAPP_Attachment(Phone As String, Msg As String, FileName As String)
    Dim sendIntent As Intent

    Try
     
        If File.Exists(File.DirInternal,FileName) Then
            File.Delete(File.DirInternal,FileName)
        End If

        File.Copy(File.DirAssets,FileName, File.DirInternal, FileName)
 
        sendIntent.Initialize(sendIntent.ACTION_MAIN,"")
      '''  sendIntent.PutExtra("jid",Phone & "@s.whatsapp.net")  >>>> REMOVE
        sendIntent.Action=sendIntent.ACTION_SEND
        sendIntent.SetPackage("com.whatsapp")
        sendIntent.SetComponent("android/com.android.internal.app.ResolverActivity")
        sendIntent.PutExtra("android.intent.extra.TEXT",Msg)
        sendIntent.PutExtra("android.intent.extra.STREAM", File.Combine(File.DirInternal,FileName))
        sendIntent.SetType("application/pdf")

        StartActivity(sendIntent)

    Catch

        Log(LastException.message)

    End Try
 
End Sub
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Hi, I use it with PDF and it works, but I saw that you changed the code, you removed the comments.

As for Wu, I don't have the business version, I can't test.
B4X:
Dim FileName As String = "documento.pdf"  'change it
Sub Send_WhatsAPP_Attachment(Phone As String, Msg As String, FileName As String)
    Dim sendIntent As Intent

    Try
    
        If File.Exists(File.DirInternal,FileName) Then
            File.Delete(File.DirInternal,FileName)
        End If

        File.Copy(File.DirAssets,FileName, File.DirInternal, FileName)
 
        sendIntent.Initialize(sendIntent.ACTION_MAIN,"")
      '''  sendIntent.PutExtra("jid",Phone & "@s.whatsapp.net")  >>>> REMOVE
        sendIntent.Action=sendIntent.ACTION_SEND
        sendIntent.SetPackage("com.whatsapp")
        sendIntent.SetComponent("android/com.android.internal.app.ResolverActivity")
        sendIntent.PutExtra("android.intent.extra.TEXT",Msg)
        sendIntent.PutExtra("android.intent.extra.STREAM", File.Combine(File.DirInternal,FileName))
        sendIntent.SetType("application/pdf")

        StartActivity(sendIntent)

    Catch

        Log(LastException.message)

    End Try
 
End Sub
thanks.
tried it - it does not work - i get a toast message that i can't share and to try later
something is going wrong...
 
Upvote 0
Top