Android Question [Solved] Problem in Sending email

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I got an error when using email with attachment. B4A 8.30, device Oreo 8.0, phone library version 2.5.

Here is codes in Main
B4X:
Process Global
   Public PUB_InstDir as String
End Sub

Codes in Starter
B4X:
Sub Process_Globals
    Public rp As RuntimePermissions
End Sub

Sub Service_Create
    Main.PUB_InstDir= rp.GetSafeDirDefaultExternal("")
End Sub

Codes in activity that send mail
B4X:
Sub SendMail
   File.Copy(Main.PUB_InstDir,"myfile.hny",File.DirRootExternal,"test.bok")
   Private Msg As Email
   Private p As Phone
   If p.SdkVersion >=24 Then 
      Msg.Attachments.Add(CreateFileProviderUri(Main.PUB_InstDir,"myfile.hny"))
   Else
       Msg.Attachments.Add(File.Combine(Main.PUB_InstDir,"myfile.hny"))
   End If

   Msg.To.Add("test@gmail.com")
   Msg.Subject = "Mail Subject "
   Msg.Body = "Mail Body"
   StartActivity(Msg.GetIntent)
End Sub

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

Codes in manifest editor
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,
   <external-files-path name="name" path="" />
)

On Oreo 8.0, when Sub SendMail called, file test.bok created in root external folder,then app just quit.
Log file error : java.lang.reflect.InvocationTargetException

Any hints how to fix this problem?
Thanks in advance.
 
Last edited:

incendio

Well-Known Member
Licensed User
Longtime User
Log(rp.GetSafeDirDefaultExternal("")) : /storage/emulated/0/Android/data/b4a.example/files
Log(File.DirInternal) : /data/user/0/b4a.example/files
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Good news, it worked, button shared clicked worked as expected.

But I want to send email attachment, which part should I change?

You use File.DirInternal ? Not using RuntimePermissions ?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…