Android Question Using Activity.StartingIntent to open an XML file via a URI

Misterbates

Active Member
Licensed User
I'm stuck (and therefore missing something obvious).

My app saves its content to XML files and I'm working on the functionality to share a saved file and to open a file shared by someone else. I'd like the "open from someone else" functionality to use Activity.StartingIntent to respond to VIEW, EDIT or SEND xml mime types (the App should be recognised as an XML filetype handler).

After reading multiple posts from those who've gone before (thanks!) I have the following in Manifest Editor:
B4X:
AddActivityText(Main,
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.EDIT" />
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/xml" />
    <data android:mimeType="application/xml" />
</intent-filter>)

And I have the following in Activity_Resume for the Main activity:
B4X:
Sub Activity_Resume
    Dim StartingIntent As Intent = Activity.GetStartingIntent
    Log(StartingIntent)
    If IsRelevantIntent(StartingIntent) Then
        Select StartingIntent.Action
        Case StartingIntent.ACTION_VIEW
            Dim sURI As String = StartingIntent.GetData
            Log(sURI)
        Case StartingIntent.ACTION_EDIT
        Case StartingIntent.ACTION_SEND
        End Select
    Else
        UpdateDisplay
    End If 
End Sub
Private Sub IsRelevantIntent(in As Intent) As Boolean
    If in.IsInitialized And in <> oldIntent Then
        If in.Action = in.ACTION_VIEW Or in.Action = in.ACTION_EDIT Or in.Action = in.ACTION_SEND Then
            oldIntent = in
            Return True
        End If
    End If
    Return False
End Sub

Sending myself an xml file attached to an email, opening the email on my device and clicking the attachment, I see my app in the list and I chose to "just once" open the xml file with my app.

I get the following in the log:
B4X:
(Intent) Intent { act=android.intent.action.VIEW dat=content://com.google.android.gm.email.attachmentprovider/3/4920/RAW typ=text/xml flg=0x13080001 cmp=scb.misterbates.intervalsplus.dev/.main }
content://com.google.android.gm.email.attachmentprovider/3/4920/RAW

But then I'm stuck. How do I open the XML file using the content:// uri?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Misterbates

Active Member
Licensed User
Nothing so simple. If I try to read the URI content as if it were a full filepath (by splitting off the "filename" at the end then opening using file.readstring), I get:
** Activity (main) Resume **
(Intent) Intent { act=android.intent.action.VIEW dat=content://com.google.android.gm.email.attachmentprovider/3/4920/RAW typ=text/xml flg=0x13080001 cmp=scb.misterbates.intervalsplus.dev/.main }
content://com.google.android.gm.email.attachmentprovider/3/4920/RAW
Error occurred on line: 451 (clsStorage)
java.io.FileNotFoundException: content:/com.google.android.gm.email.attachmentprovider/3/4920/RAW (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:209)
at anywheresoftware.b4a.objects.streams.File.ReadString(File.java:271)
at scb.misterbates.intervalsplus.dev.clsstorage._importfromuri(clsstorage.java:1327)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
at scb.misterbates.intervalsplus.dev.main._actimport(main.java:1026)
at scb.misterbates.intervalsplus.dev.main._activity_resume(main.java:1468)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at scb.misterbates.intervalsplus.dev.main$ResumeMessage.run(main.java:302)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

Seems the URI can't be opened as if it were a filename ...
 
Upvote 0
Top