Android Question opening file attachment from gmail client

Branko Milosevic

Active Member
Licensed User
my app successfully registers a custom type file (.imp) and it's registered as one of the apps that can deal with this type of file. G-mail in fact skips the options and opens my app directly.

I am also catching the intent in my activity with this code:
B4X:
Sub Activity_Resume
  
    Dim i As Intent
    i = Activity.GetStartingIntent
  
    If  i.Action.Contains("VIEW") Then
  
            Log(i.GetData)
      
    End If

End Sub

When I click on the file (with my custom extension) in the File Manager app, my app starts and the log(i.GetData) prints the file URI as:
B4X:
file:///sdcard/impromptu/example%203.imp
However from g-mail the log(i.GetData) does not show a unique file name. This is the log:
B4X:
content://gmail-ls/[email protected]/messages/29/attachments/0.1/BEST/false
I obviously need some other way to access the attached file but so far haven't been able to find any.

Thanks a lot if you can help...
 

Branko Milosevic

Active Member
Licensed User
OK I have a working solution here:
B4X:
Dim i As Intent
    i = Activity.GetStartingIntent
   
    If  i.Action.Contains("VIEW") Then
   
        Dim jo As JavaObject
        Dim ContentDir As String = jo.InitializeStatic("anywheresoftware.b4a.objects.streams.File").GetField("ContentDir")
        Dim strUri As String = i.GetData

        Dim Inp As InputStream  = File.OpenInput(ContentDir , strUri)
        Dim Out As OutputStream = File.OpenOutput(File.DirRootExternal&"/impromptu", "testfile.imp", False)

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

However this does not retrieve the original file name and copies the file in the "testfile.imp" file. If I don't figure this out I am may have to implement a sort of a unique file name generator or pop an input dialog to the user to name the file.
 
Upvote 0

Branko Milosevic

Active Member
Licensed User
Would intent action "SEND" instead of "VIEW" make a difference?

How to get extras? I am not at the computer with b4a at the moment but thinking i.GetExtras would do?
 
Upvote 0
Top