B4A Class [Class] Send E-Mail with FileproviderURI

Hi,

1) The library "Runtime Permissions" is necessary for this
2) put this in your manifest

B4X:
'************ File Provider ****************
'E-Mail senden ab Android 6+
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,
   <external-files-path name="name" path="" />
)
'Ende of File Provider

Put in an Activity:
B4X:
Sub Globals
    Private sendmail As clsSendMail
End Sub

Sub Activity_Create(FirstTime As Boolean)

   'E-Mail classe initialisieren
   sendmail.Initialize
End Sub

Sub btnSend_Click
       sendmail.Send(File.DirInternal, "body", "SUBJECT", "FileName")
End Sub

Class clsSendMail:
B4X:
Sub Class_Globals
    Private rp As RuntimePermissions

    Private shared As String
    Private GlobPath As String
    Private GlobFileName As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    shared = rp.GetSafeDirDefaultExternal("")
End Sub

Public Sub Send(dir As String, body As String, SendSub As String, Attachments As String)
    Dim SendAtt As String = ""
    GlobPath = dir
    GlobFileName = Attachments
    
    Dim SendTo As String = "" '"myeMail@myDomain.com"
    
    If Attachments <> "" Then
        File.Copy(GlobPath, GlobFileName, shared, GlobFileName) 'Put the file in a shared space ready for attaching
        SendAtt = File.Combine(GlobPath, GlobFileName)
    End If
 
    SendEmail (SendTo, body, SendSub, SendAtt)
End Sub

Private Sub SendEmail (SendTo As String, SendBody As String, SendSub As String, SendAtt As String)
    Dim EmailIntent As Intent
    
    LogColor("Send Att: " & SendAtt, Colors.Red)

    EmailIntent.Initialize(EmailIntent.ACTION_SEND, SendTo)
    EmailIntent.putExtra("android.intent.extra.SUBJECT", SendSub)
    EmailIntent.putExtra("android.intent.extra.TEXT", SendBody)
    EmailIntent.SetType("text/plain") 'it is not related to the file itself.
    EmailIntent.Flags = 1
    If SendAtt <> "" Then
        EmailIntent.PutExtra("android.intent.extra.STREAM", CreateFileProviderUri(shared, GlobFileName))
    End If

    StartActivity (EmailIntent)
End Sub

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

Main Thank get to Erel.
I derived the class from this example.
 
Top