Android Question Can not find shared folder

Mostez

Well-Known Member
Licensed User
Longtime User
my application saves CSV file then it opens it with associated application
I use this code to get storage folder
B4X:
Dim StorageDir As String = Starter.Provider.SharedFolder

B4X:
Public Sub Initialize
    Dim p As Phone
    If p.SdkVersion >= 24 Or File.ExternalWritable = False Then
        UseFileProvider = True
        SharedFolder = File.Combine(File.DirInternal, "shared")
        File.MakeDir("", SharedFolder)
    Else
        UseFileProvider = False
        SharedFolder = rp.GetSafeDirDefaultExternal("shared")
    End If
    Log($"Using FileProvider? ${UseFileProvider}"$)
End Sub
B4X:
File.WriteString(StorageDir, FileName, Sb.ToString )

B4X:
Dim in As Intent
in.Initialize(in.ACTION_VIEW, "")
Starter.Provider.SetFileUriAsIntentData(in, Filename)
in.SetType("text/csv")
StartActivity(in)
I ran the application on two different phones, one runs Android 10 the code retuned "/data/user/0/com.etripClient/files/shared"
the other one runs Android 6 and the code returned "/storage/emulated/0/Android/data/com.etripClient/files/shared"
every thing is OK with the Android 6 phone, with the other one, it opens the file with associated application, but I searched for saved file, I did not find it any where on phone storage or SD card, I did not even find 'com.etripclient' folder!

how to fix this?
 

Mostez

Well-Known Member
Licensed User
Longtime User
thanks it works OK
EDIT: file saved ok, folder and file are visible now, but failed to open file with associated application

i changed code to this:
B4X:
Public Sub Initialize
    Dim p As Phone
    If p.SdkVersion >= 24 Or File.ExternalWritable = False Then
        UseFileProvider = True
        'SharedFolder = File.Combine(File.DirInternal, "shared")
        Dim rp As RuntimePermissions
        SharedFolder = rp.GetSafeDirDefaultExternal("shared")
        File.MakeDir("", SharedFolder)
    Else
        UseFileProvider = False
        SharedFolder = rp.GetSafeDirDefaultExternal("shared")
    End If
    Log($"Using FileProvider? ${UseFileProvider}"$)
End Sub

but this code failed to run, and I get error exception
B4X:
Dim in As Intent
in.Initialize(in.ACTION_VIEW, "")
Starter.Provider.SetFileUriAsIntentData(in, Filename)
in.SetType("text/csv")
StartActivity(in)

here is code on line 40:
B4X:
'Replaces the intent Data field with the file uri.
'Resets the type field. Make sure to call Intent.SetType after calling this method
Public Sub SetFileUriAsIntentData (Intent As Intent, FileName As String)
    Dim jo As JavaObject = Intent
    jo.RunMethod("setData", Array(GetFileUri(FileName)))
    Intent.Flags = Bit.Or(Intent.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
End Sub

Error occurred on line: 40 (FileProvider)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at com.etripClient.fileprovider._getfileuri(fileprovider.java:155)
at com.etripClient.fileprovider._setfileuriasintentdata(fileprovider.java:106)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
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.raiseEvent(BA.java:193)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1760)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:989)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.etripClient/files/shared/tt.CSV
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
... 23 more
(Exception) java.lang.Exception: java.lang.reflect.InvocationTargetException
** Activity (triprequests) Pause, UserClosed = false **
 

Attachments

  • FileProvider.bas
    1.9 KB · Views: 131
Last edited:
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
The path does not matter, what matters is to make files visible, just in case associated applications did no work or things went wrong user still able to access these files again
 
Upvote 0
Top