Android Question Email intent + attachment: email clients only are required

peacemaker

Expert
Licensed User
Longtime User
HI, All

Is it possible ?
Email object of Phone lib offers all text-messaging apps - it's wrong, the attachment is required by email only.
 

peacemaker

Expert
Licensed User
Longtime User
B4X:
          Dim i As Intent '= email1.GetIntent
                    i.Initialize("android.intent.action.SENDTO", "mailto:")
                    i.PutExtra("subject", Application.LabelName & "_v." & Application.VersionName & " Android: support request")
                    i.PutExtra("android.intent.extra.EMAIL", Array As String (Starter.PublicEmail))

                    Dim hw1 As Phone
                    Dim body As String = CRLF & CRLF & CRLF & CRLF & Application.LabelName & " v." & Application.VersionName & CRLF & "Manufacturer=" & hw1.Manufacturer & "; Model=" & hw1.Model & "; Product=" & hw1.Product & "; SdkVersion=" & hw1.SdkVersion & CRLF
                   
                    Dim res As Int = Msgbox2("Add technical logs ?", "If help is needed:", "OK", "Cancel", "Do not add", Null)
                    If res = DialogResponse.CANCEL Then
                        Return
                    Else if res = DialogResponse.POSITIVE Then
                        body = body & CRLF & Starter.logs
                    End If
                    i.PutExtra("android.intent.extra.TEXT", Array As String (body))   'THIS DOES NOT WORK :(
                    i.WrapAsIntentChooser("Email:")
                    StartActivity(i)

In this code only EMAIL apps are chosen, but the email body is not pre-set :( And here no attachments are possible, as another action.
Any help ?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Here i see that no attachs possible, but why body text cannot be pre-set ?
Task is "just simple" - offer only EMAIL apps for manual usage of user, but with all pre-set fields: To, Subject, body.

Phone.Email is not for this.

The super-task - is the same, but + attaches. As only by Email attaches can be sent, in whole, and other text-messaging apps again must be passed.
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
2 variants are needed, as described above:
1) without attachments, but all fields
2) and with attachments, as the universal super-task (if it's possible with optional attachs - it's the topic aim).
But for only EMAIL apps, not any txt-messaging.

Trouble is just if you try to use Phone.Email - lots of apps are offered, and attach cannot be sent for sure.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Ha! SOLVED !

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="" />
)

B4X:
'SendAtt = map CreateMap("filename1": "path1", "filename2": "path2")
Sub SuperSendEmail (SendTo As List, SendBody As String, SendSubject As String, SendAtt As Map)
    If SendTo.IsInitialized = False Then
        ToastMessageShow("Email cannot be sent: empty email-address", True)
        Return
    End If
    Dim FinalEmailIntent As Intent
    Dim MyEmail As Email
    MyEmail.To = SendTo
    MyEmail.Body = SendBody
    MyEmail.Subject = SendSubject
    If SendAtt.IsInitialized Then
        For i = 0 To SendAtt.Size - 1
            MyEmail.Attachments.Add(CreateFileProviderUri(SendAtt.GetValueAt(i), SendAtt.GetKeyAt(i)))
        Next
    End If
    FinalEmailIntent = MyEmail.GetIntent
    FinalEmailIntent.SetType("message/rfc822")  'THE TROUBLE ROOT !!!!
    FinalEmailIntent.WrapAsIntentChooser("Email:")
    StartActivity (FinalEmailIntent)
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

Usage:
B4X:
Dim rp As RuntimePermissions
Dim Folder As String = rp.GetSafeDirDefaultExternal("")  'folder like declared in the manifest - the root of the external app folder
File.WriteString(Folder, "att.txt", att_file_content)
Dim atts As Map = CreateMap("att.txt": Folder)
others.SuperSendEmail(Array As String(Starter.PublicEmail), Body, Subject, atts)
 
Last edited:
Upvote 0
Top