Android Question File Uri Exposed Error

peggjones

Active Member
Licensed User
Longtime User
I have just changed an app to target SDK version 26 and am getting the above error. Is there anyway to just suppress this error rather than recoding to use file picker? It will involve quite a lot of work for me (I have several app that would need changing.
 

peggjones

Active Member
Licensed User
Longtime User
Thanks. I that would be the case. Is there any documentation for fileprovider. I've searched the forum and can only find examples. I want to be able to add file names and paths to a database and later open and display the documents. Many thanks.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

peggjones

Active Member
Licensed User
Longtime User
I am having a problem at the moment with displaying a word document the address of which is held on database.

The error I get is java.lang.reflect.InvocationTargetException

B4X:
    filepath1 = Main.SQL.execQuerySingleResult("SELECT path FROM Attachments where attachno = " & QUOTE & Main.attachno & QUOTE)
    filename1 = Main.SQL.execQuerySingleResult("SELECT filename FROM Attachments where attachno = " & QUOTE & Main.attachno & QUOTE)
    filepath1 = filepath1 & "/" & filename1
   
' Dummy is just a convenient file to have put in DIRASSETS.
' Copy file from Database to DIRASSETS

    File.Copy(File.DirAssets, "Dummy.docx", Starter.shared, filename1)
    Dim in As Intent
   
' Get URL of the file now in DIRASSETS

    in.Initialize(in.ACTION_VIEW, CreateFileProviderUri(Starter.shared, "Dummy.docx"))
    in.Flags = 1

Sub CreateFileProviderUri (dir As String, filename2 As String) As 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, filename2))
    Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))

End Sub

Many Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The error I get is java.lang.reflect.InvocationTargetException
You should post the full error message from the logs.
Also write which line causes this error.

Tip:
You should never write:
B4X:
filename1 = Main.SQL.execQuerySingleResult("SELECT filename FROM Attachments where attachno = " & QUOTE & Main.attachno & QUOTE)
You should write:
B4X:
filename1 = Main.SQL.execQuerySingleResult2("SELECT filename FROM Attachments where attachno = ?", Array As String(Main.attachno))
 
Upvote 0

peggjones

Active Member
Licensed User
Longtime User
Thanks for tip I'll do it like that in future.

The line causing the error is

Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))

The full error message is

20180328192049_IMG_0055.JPG
/storage/emulated/0/bossfolder/VocabDocs
PronunciationNotes.doc
Error occurred on line: 580 (Listattachments)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
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:339)
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 b4a.Checklist.listattachments._createfileprovideruri(listattachments.java:1374)
at b4a.Checklist.listattachments._docbutt_click(listattachments.java:1452)
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.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6257)
at android.widget.TextView.performClick(TextView.java:11149)
at android.view.View$PerformClick.run(View.java:23705)
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:6780)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:604)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:578)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:416)
... 31 more
** Service (starter) Destroy **
 
Upvote 0

peggjones

Active Member
Licensed User
Longtime User
I think I need to understand more about this whole process. I have very limited knowledge. It took me ages to get the process to work using the "old" method like this

B4X:
    Dim I As Intent
    I.Initialize(I.ACTION_VIEW, "file:///" & filepath)
    I.SetComponent(" ")
    StartActivity(I)

I am now trying to update my so that it will work targeting SDK 26.

The problem I have (which is entirely my fault) is that with my lack of knowledge I am having trouble adapting the sample code to work with a path from my database

B4X:
Sub Button1_Click
    'copy the file to the shared folder
    File.Copy(File.DirAssets, "b4a.png", Starter.shared, "b4a.png")
    Dim in As Intent
    in.Initialize(in.ACTION_VIEW, CreateFileProviderUri(Starter.shared, "b4a.png"))
    in.Flags = 1
    StartActivity(in)
End Sub


Sub CreateFileProviderUri (Dir As String, FileName As String) As 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

I'll go back to the drawing board! Don't spend any more time on it.

Many Thanks for trying
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…