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

Discussion in 'Android Questions' started by peacemaker, Jun 14, 2018.

  1. peacemaker

    peacemaker Well-Known Member Licensed 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.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. peacemaker

    peacemaker Well-Known Member Licensed User

    Code:
    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 ?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    This will not work if you need attachments. A different approach is to use PackageManager.QueryIntentActivities and try to filter out unwanted apps.
     
  5. peacemaker

    peacemaker Well-Known Member Licensed 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: Jun 17, 2018
  6. Erel

    Erel Administrator Staff Member Licensed User

    Do you need attachments or not? If so then the intent you are using will not help.
     
  7. peacemaker

    peacemaker Well-Known Member Licensed 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.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    peacemaker likes this.
  9. peacemaker

    peacemaker Well-Known Member Licensed User

    Ha! SOLVED !

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

    Code:
    '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 StringAs 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:
    Code:
    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: Jun 17, 2018
    Dave O likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice