Android Question No attachment [SOLVED]

AHilberink

Active Member
Licensed User
Longtime User
Hi,

I try to add an attachment to a mail intent.

I use:
B4X:
Sub btnMail_Click
    Dim in As Intent
    
    btnMail.Visible=False
    Dim PanelShot As B4XBitmap = ScrollView2D1.Panel.As(B4XView).SnapShot
    ImageView1.Visible=True
    Dim Out2 As OutputStream = File.OpenOutput(xui.DefaultFolder, "Geleide"&Formuliernr.Text&".jpg", False)
    PanelShot.WriteToStream(Out2, 50, "JPEG")
    Out2.Close
    
    ImageView1.Bitmap=LoadBitmap(xui.DefaultFolder,"Geleide"&Formuliernr.Text&".jpg")
    
    Log(xui.FileUri(xui.DefaultFolder,"Geleide"&Formuliernr.Text&".jpg"))
    in.Initialize(in.ACTION_SEND,"mailto:andre@xx.nl")
    in.PutExtra("android.intent.extra.SUBJECT", "this is the subject")
    in.PutExtra("android.intent.extra.TEXT", "this is the body")
    in.PutExtra("android.intent.extra.STREAM", xui.FileUri(xui.DefaultFolder,"Geleide"&Formuliernr.Text&".jpg"))
    StartActivity(in)
    btnMail.Visible=True
End Sub

ImageView1 is filled by the file, so he exist but no attachment. Using Outlook App.

Can someone help?

Kind regards,
André
 

zed

Active Member
Licensed User
Try this
B4A:
EmailIntent.PutExtra("android.intent.extra.STREAM", CreateFileProviderUri(Dir, FileName))

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

AHilberink

Active Member
Licensed User
Longtime User
Thanks guys. It works.

I did the following:
Add to Manifest:
B4X:
AddManifestText(<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />
)

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)

Changed code to:
B4X:
Sub btnMail_Click
    Dim FileName As String = "Geleide"&Formuliernr.Text&".jpg"
    btnMail.Visible=False
    Dim PanelShot As B4XBitmap = ScrollView2D1.Panel.As(B4XView).SnapShot
    ImageView1.Visible=True
    Dim Out2 As OutputStream = File.OpenOutput(Provider.SharedFolder,FileName, False)
    PanelShot.WriteToStream(Out2, 50, "JPEG")
    Out2.Close
    
    Dim email As Email
    email.To.Add("andre@xx.nl")
    email.Subject = "this is the subject"
    email.Attachments.Add(Provider.GetFileUri(FileName))
    Dim in As Intent = email.GetIntent
    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    StartActivity(in)
    btnMail.Visible=True
End Sub

Kind regards,
André
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…