Android Question File provider error

andyp

Member
Licensed User
Hi

Using FileProvider
https://www.b4x.com/android/forum/threads/class-fileprovider-share-files.97865/#content

The example app works no problem.

I have used the code (and manifest, b4a.png file, etc) in my app.

I get this error log:

Error occurred on line: 34 (FileProvider)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at f3azone.pro.reader.fileprovider._getfileuri(fileprovider.java:127)
at f3azone.pro.reader.loadthebox$ResumableSub_Button3_Click.resume(loadthebox.java:623)
at f3azone.pro.reader.loadthebox._button3_click(loadthebox.java:546)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:352)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12693)
at android.view.View$PerformClick.run(View.java:26100)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6942)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/f3azone.pro.reader/files/shared/b4a.png
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
... 24 more

Line 34 is:
B4X:
Return fp.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))

Thoughts? Thank you.....
 

andyp

Member
Licensed User
If I make this change it appears to work, so the SharedFolder is the same all SdkVersions

But this does not seem right? Why does the example app work with the original code, and my app does not?

Thank you for any help!

B4X:
Public Sub Initialize
    Dim p As Phone
    If p.SdkVersion >= 24 Or File.ExternalWritable = False Then
        UseFileProvider = True
' add this line: 
        SharedFolder = rp.GetSafeDirDefaultExternal("shared")
' remove these two lines:
'        SharedFolder = File.Combine(File.DirInternal, "shared")
'        File.MakeDir("", SharedFolder)
    Else
        UseFileProvider = False
        SharedFolder = rp.GetSafeDirDefaultExternal("shared")
    End If
    Log($"Using FileProvider? ${UseFileProvider}"$)
End Sub
 
Upvote 0

andyp

Member
Licensed User
Humm… I have checked the manifest - I think I covered everything?

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases. change target 18 to 26
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136

AddManifestText(
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>
<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  android:maxSdkVersion="18"  />
 <uses-feature android:name="android.hardware.location.gps"/>
  )   

SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

AddActivityText(EmailedBox,
         <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
                <data android:scheme="file" />
                <data android:host="*" />
                <data android:port="*" />
                <data android:pathPattern=".*..*..*..*..*..*.f3a" />
                <data android:pathPattern=".*..*..*..*..*.f3a" />
                <data android:pathPattern=".*..*..*..*.f3a" />
                <data android:pathPattern=".*..*..*.f3a" />
                <data android:pathPattern=".*..*.f3a" />
                <data android:pathPattern=".*.f3a" />
            </intent-filter>
              <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
                <data android:scheme="content" />
                <data android:pathPattern=".*..*..*..*..*..*.f3a" />
                <data android:pathPattern=".*..*..*..*..*.f3a" />
                <data android:pathPattern=".*..*..*..*.f3a" />
                <data android:pathPattern=".*..*..*.f3a" />
                <data android:pathPattern=".*..*.f3a" />
                <data android:pathPattern=".*.f3a" />
            </intent-filter>
)

AddApplicationText(
<meta-data
  android:name="com.google.android.geo.API_KEY"

  android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

<provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
 
SetActivityAttribute(SaveTheBox, android:windowSoftInputMode, "stateVisible")
 
CreateResource(xml, provider_paths,
   <external-files-path name="name" path="shared" />
)
 
Upvote 0

andyp

Member
Licensed User
Found the problem - it was with the manifest - I missed:

B4X:
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)

Thanks for pointing me in the right direction!!
 
Upvote 0
Top