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
Full error log :

java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at gsf.mPOS.mainmenu._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2(mainmenu.java:516)
at gsf.mPOS.mainmenu._slidemenu_click(mainmenu.java:773)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1058)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:1013)
at com.myApp.slidemenu._mlistview_itemclick(slidemenu.java:215)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at anywheresoftware.b4a.BA$1.run(BA.java:330)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/gsf.mPOS/files/mpos.bok
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:679)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:378)
... 19 more
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
What is the purpose of this line:
B4X:
File.Copy(Main.PUB_InstDir,"myfile.hny",File.DirRootExternal,"test.bok")
?

The code doesn't seem to match the error message.
This is just my test to find out whether the file in installation directory can be accessed. This line can be ignored.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
On my device, when button send clicked, the app just quit.
No error message, but when run again on the second time, the log shows error like on post #3 before even the button clicked.

One of my client reported the same behavior, the app just quit right away after button send clicked.

This is happen with Oreo 8.0. On JellyBean, it worked fine.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
How can the file name be: /storage/emulated/0/Android/data/gsf.mPOS/files/mpos.bok

If you are calling it with:
B4X:
CreateFileProviderUri(PUB_INST_DIR,"image.jpg")
?

It cannot be.

Ah, you meant that line. Sorry for my misunderstanding, here is the error from sample project :

main_v6 (java line: 401)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at b4a.test.main._v6(main.java:401)
at b4a.test.main._button1_click(main.java:373)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/b4a.test/files/image.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:679)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:378)
... 18 more
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **

It was point to a different file because of different project. The post on #3 from real project, but the essence is, it was the same error.

I checked with file manager, the file image.jpg on folder /storage/emulated/0/Android/data/b4a.test/files does exists.
 
Last edited:
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Your sample works fine in API-26, 27, 28 emulators.

Did you update SDK last week and install last mavens, marked as 28.0.0 (they were in "recommended") ?
If so, I'd recommend to return previous mavens and android.jar from android-27.
Too many strange results with new mavens.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Did you update SDK last week and install last mavens, marked as 28.0.0 (they were in "recommended") ?
If so, I'd recommend to return previous mavens and android.jar from android-27.
Too many strange results with new mavens.
Most probably not related.

@incendio Does it happen every time that you run the project?

What is the output of Log(File.ExternalWritable)?
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Your sample works fine in API-26, 27, 28 emulators.

Did you update SDK last week and install last mavens, marked as 28.0.0 (they were in "recommended") ?
If so, I'd recommend to return previous mavens and android.jar from android-27.
Too many strange results with new mavens.
No, I didn't update.
Most probably not related.

@incendio Does it happen every time that you run the project?

What is the output of Log(File.ExternalWritable)?

Yes, it happen every time.

The Output for Log(File.ExternalWritable) is true.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
I am running on real device.

Your sample gave an error

Logger connected to: HUAWEI LLD-L21
--------- beginning of main
--------- beginning of system
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
android.support.v4.content.FileProvider$SimplePathStrategy@29b452a
{}
** Activity (main) Resume **
main_v0 (java line: 406)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at b4a.example.main._v0(main.java:406)
at b4a.example.main._button1_click(main.java:380)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/b4a.example/files/shared/b4a.png
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:679)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:378)
... 18 more
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
android.support.v4.content.FileProvider$SimplePathStrategy@29b452a
{}
** Activity (main) Resume **

I am compile it with B4A 8.30, this is not the caused, right?
 
Last edited:
Upvote 0
Top