Android Question Launch new email (compose)?

MotoMusher

Active Member
Licensed User
Longtime User
Guessing this has to be done with an intent somehow.

I have a utility which generates a file for the user, which they must then open on their pc, not phone. Rather than the user opening gmail, the finding the file in the file system, is there a way for me to pop up a new blank email with only my file attached? I don't care if the first time they have to pick from all their apps to set a default, that would be fine.

Any ideas?
 

NJDude

Expert
Licensed User
Longtime User
Try this code:
B4X:
Dim i As Intent
 
i.Initialize(i.ACTION_SEND, "")
 
i.SetType("text/plain")
 
i.PutExtra("android.intent.extra.SUBJECT", "The Subject")
i.PutExtra("android.intent.extra.TEXT", "The Body")
 
i.SetType("image/png")
i.PutExtra("android.intent.extra.STREAM", CreateUri("file://" & File.Combine(File.DirDefaultExternal, "File_To_Attach.png")))
 
StartActivity(i)
 
...
 
Sub CreateUri(uri As String) As Object
 
    Dim r As Reflector
 
    Return r.RunStaticMethod("android.net.Uri", "parse", Array As Object(uri), Array As String("java.lang.String"))
 
End Sub
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Try this code:
B4X:
Dim i As Intent
i.Initialize(i.ACTION_SEND, "")
i.SetType("text/plain")
i.PutExtra("android.intent.extra.SUBJECT", "The Subject")
i.PutExtra("android.intent.extra.TEXT", "The Body")
i.SetType("image/png")
i.PutExtra("android.intent.extra.STREAM", CreateUri("file://" & File.Combine(File.DirDefaultExternal, "File_To_Attach.png")))
...

NJDude - After reading your post I checked out the email code in an app I'm working on and it's something like this:

B4X:
Sub SendEmail(txt As String)
    Dim msg As Email
    msg.To.Add("somebody@url")
    msg.SubJect("some subject")
    msg.Body = txt
    StartActivity(msg.GetIntent)
End Sub

It's been so long since I put that code in that I don't remember where it came from. Is one method any better than the other?
 
Upvote 0

ComposerB4A

Member
Licensed User
Longtime User
Hello, i run both excellent codes in devices (phone and tablet)
["i.Initialize("i.ACTION_SEND")"... and "msg.To.Add("somebody@url"...],
and the first code is more compatible with devices, because the second code in some devices show error "Not activity found to handle intent"
depends on what type of device is targeted the code, in all case excellent codes
 
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
NJDude - After reading your post I checked out the email code in an app I'm working on and it's something like this:

B4X:
Sub SendEmail(txt As String)
    Dim msg As Email
    msg.To.Add("somebody@url")
    msg.SubJect("some subject")
    msg.Body = txt
    StartActivity(msg.GetIntent)
End Sub

It's been so long since I put that code in that I don't remember where it came from. Is one method any better than the other?

This worked nicely. Although the msg.SubJect("some subject") gave me some issues, it seemed like it was looking for an array.
 
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
lol, ok got the msg.subject working.

How about the msg.attachment? I just need to attach one file> File.DirDefaultExternal, "feedback.db"
 
Upvote 0

metrick

Active Member
Licensed User
Longtime User
This work for me.
msg.Attachments.Add(File.Combine(File.DirRootExternal, "FolderName/FileName.txt" ) )
 
Upvote 0
Top