Android Question (SOLVED)File path to URI path

Ferdari

Active Member
Licensed User
Longtime User
Hi everyone

Im creating an app that need to save URI path to a JSON file, is there a function to return the URI from a file?

for example my json should contain uri like this:
file:///data/user/0/com.myapp/files/share/1/0.webp
My files are stored on the safe dir share created
XML:
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />
)
All folders are ok and work, but i still not realize how to extract the URI from a file, for example the file sound.mp3 saved at shared folder
B4X:
safefolder = Starter.Provider.SharedFolder
mp.Load(safefolder,"sound.mp3")
I want to get his URI path and save it to file for later working with it.
 

peacemaker

Expert
Licensed User
Longtime User
If you copied each needed file into "safefolder" - each file path will be "safefolder/filename", i.e. "shared" subfolder in the internal folder of your app, something like "data/user/0/com.myapp/files/shared/sound.mp3"
 
Upvote 0

Ferdari

Active Member
Licensed User
Longtime User
If you copied each needed file into "safefolder" - each file path will be "safefolder/filname", i.e. "shared" subfolder in the internal folder of your app
Ok, but how to the get the real URI:
file:///data/user/0/com.myapp/files/share/1/sound.mp3
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Strange question... URI of the file in your app folders is just file's path
B4X:
Dim URI as String = "file:///" & safefolder & "/sound.mp3"
 
Upvote 0

Ferdari

Active Member
Licensed User
Longtime User
I just found this function on my FileProvider class, if someone want to know the URI of a file use this function:
B4X:
'Returns the file uri.
Public Sub GetFileUri (FileName As String) As Object
   
    If UseFileProvider = False Then
        Dim uri As JavaObject
        Return uri.InitializeStatic("android.net.Uri").RunMethod("parse", Array("file://" & File.Combine(SharedFolder, FileName)))
    Else
        Dim f As JavaObject
        f.InitializeNewInstance("java.io.File", Array(SharedFolder, FileName))
        Dim fp As JavaObject
        Dim context As JavaObject
        context.InitializeContext
        fp.InitializeStatic("android.support.v4.content.FileProvider")
        Return fp.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
    End If
End Sub

it returns the uri like this:
GetFileUri(File.DirAssets & "/file.mp3")
content://com.app.provider/name/AssetsDir/file.mp3
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…