Android Question Get file from email attachment with filename

mrossen

Active Member
Licensed User
Longtime User
Hi,

I have make this example to work: https://www.b4x.com/android/forum/threads/get-file-from-email-attachment.54224/#post-340537

This is pretty smart :)

In thread #3 there is this code:

B4X:
Dim Intent1 As Intent
    Intent1 = Activity.GetStartingIntent

    If Intent1.Action = "android.intent.action.VIEW" Then    ' The activity has been called from outside the app
      
        Dim jo As JavaObject
        Dim cd As String = jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File").GetField("ContentDir")
        Dim UriString As String = intent1.GetData

        Dim Inp As InputStream  = File.OpenInput(cd , UriString)
        Dim Out As OutputStream = File.OpenOutput(myFolder, myFile, False)

        File.copy2(Inp, Out)
        Inp.Close
        Out.Close
    End if

And in the line :

B4X:
Dim Out As OutputStream = File.OpenOutput(myFolder, myFile, False)

I can exchange myFolder with "File.DirInternal" go get the file in my internal dir.

But how do I get the original filename from the E-Mail?

Mogens
 

drgottjr

Expert
Licensed User
Longtime User
it's in the email headers. you need to search for and download the MailParser class for B4A. one of the methods exposes an attached file's name. MailParser does a pretty good job of plowing through email format
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi again,

I don't think I am going the right way here.

I have tried this, but can not get it to work

B4X:
Sub GetPathFromContentResult(UriString As String) As String
    
    If UriString.StartsWith("/") Then Return UriString 'If the user used a file manager to choose the image
    Dim Cursor1 As Cursor
    Dim Uri1 As Uri
    Dim Proj() As String = Array As String("_data")
    Dim cr As ContentResolver
    cr.Initialize("")
    If UriString.StartsWith("content://com.google.android.gm.sapi") Then
        Dim i As Int = UriString.IndexOf("%3A")
        Dim id As String = UriString.SubString(i + 3)
        Uri1.Parse("message_attachment_external")
        Cursor1 = cr.Query(Uri1, Proj, "_display_name = ?", Array As String(id), "")
    Else
        Uri1.Parse(UriString)
        Cursor1 = cr.Query(Uri1, Proj, "", Null, "")
    End If
    Cursor1.Position = 0
    Dim res As String
    res = Cursor1.GetString("_data")
    Cursor1.Close
    Return res
    
End Sub

And I dont' know what Erel means with : "Note that this is no longer required. You can just pass "ContentDir" as the folder parameter."

Mogens
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
have tried this, but can not get it to work
This is not enough information for us to help you.

: "Note that this is no longer required. You can just pass "ContentDir" as the folder parameter."
B4X:
Dim cd As String = jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File").GetField("ContentDir")
        Dim UriString As String = intent1.GetData

        Dim Inp As InputStream  = File.OpenInput(cd , UriString)

=>

B4X:
        Dim UriString As String = intent1.GetData
        Dim Inp As InputStream  = File.OpenInput("ContentDir", UriString)
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi again

I attached a sample project

The uri I get looks like this:
UriString: content://com.google.android.gm.sapi/[email protected]/message_attachment_external/%23thread-f%3A1650098495922933549/%23msg-f%3A1650098495922933549/0.1?account_type=com.google&mimeType=application%2Foctet-stream&rendition=1

Should the filename be in this line in plain text?

Mogens
 

Attachments

  • mail.zip
    9.4 KB · Views: 236
Upvote 0
Top