Android Question How to share a JPEG with whatsapp API

How can I share an image JPEG and not only text using the api and Intent?

B4X:
Intent1.Initialize(Intent1.ACTION_VIEW, "https://api.whatsapp.com/send?data-action=File://" & File.Combine(File.DirRootExternal, fileName))
 

asales

Expert
Licensed User
Longtime User
B4X:
Starter:
Public SharedFolder As String
SharedFolder = rp.GetSafeDirDefaultExternal("shared")

Sub PostWhatsapp
    Dim intent As Intent
    intent.Initialize(intent.ACTION_SEND,"")
    intent.SetType("image/jpg")
    
    Dim img As String = "myimage.jpg"
    
    intent.SetComponent("com.whatsapp/.ContactPicker")
    
    intent.PutExtra("android.intent.extra.STREAM", CreateFileProviderUri(Starter.SharedFolder, img))

    StartActivity(intent)
End Sub

Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
    Dim FileProvider2 As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    FileProvider2.InitializeStatic("android.support.v4.content.FileProvider")
    Dim f As JavaObject
    f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
    
    Return FileProvider2.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End Sub
 
Upvote 0
Top