Android Question ContentChooser - get the URI of a given filename after app-restart

Michael Müller Anywhere

Member
Licensed User
Longtime User
Hello,
In my app, users are supposed to select a sound file from the audio section to play when a message arrives. I store both the filename and the URI in a database.
After restarting the app, the URI is no longer valid/persistant. How can I determine a valid URI from the filename (mysoundfile.mp3)?
This app is for the play store.
Thanks


Get the URI:
Private Sub but_fileselect_Click
    Dim CC As ContentChooser 
    Dim SQL As String  
    Dim result As Int
    Dim Datei_pur As String
   
    CC.Initialize("cc")
    CC.Show("audio/*", "Datei auswählen")
    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)



    If Success And FileName.Length > 0 Then
        Datei_pur = GetFileInfoByIndex("_display_name", FileName)
        Msgbox2Async("Bisherige Datei: <" & mod_allgemein.set_Externe_SoundDatei_Klartext & "> ersetzen durch: <" & Datei_pur & ">", "Sound-Datei auswählen", "Ja", "", "Nein", Null, False)
        Wait For MsgBox_Result(result As Int)
        If result = DialogResponse.Positive Then
            'URI in DB speichern
            SQL="UPDATE tab_settings SET sFrei3 = '" & Datei_pur & TAB & FileName & "' WHERE id = 1"
            mod_SQL.exec_NONQuery(SQL)
            mod_allgemein.set_Externe_SoundDatei = FileName                'z.B.: content://com.android.providers.media.documents/document/audio%3A1000007252
            mod_allgemein.set_Externe_SoundDatei_Klartext = Datei_pur
            Label_SoundDatei.Text = Datei_pur
        End If
    End If      
   
End Sub

Play the file:
Starter.MP.Load("ContentDir", mod_allgemein.set_Externe_SoundDatei)
Starter.MP.Play
 

drgottjr

Expert
Licensed User
Longtime User
i have no problem selecting the "file" using contentchooser, playing the "file", saving its location and name,
rebooting my device, restarting the app, reading its location and name (previously saved) and listening to the
file again as if selected using the contentchooser. note: i do not save the file locally, only its location and name.
i do not use the contentchooser to select the file again. the only thing i save is the location and name of the
resource, which - i believe - is what you trying to do.

UPDATE: in looking at your code, you seem to do - more or less - what i do, except it looks like you try to reload the file from a variable which no longer exists
when you restart the app (i can only see part of your code, but that's what i think). what is mod_allgemein.set_Externe_SoundDatei? is this supposed to be some kind of persistant variable or object (eg, a map or keystore)? i don't understand why you do to the trouble of saving the uri in your database, but then you don't use the database when you try to reload the file. the database would be persistant (unless you are recreating it each time). i save the uri to a text file: File.Writestring(File.DirInternal, "uri.txt", Dir & "~" & FileName). then i can read that file next time and load the uri.
after this line: Starter.MP.Load("ContentDir", mod_allgemein.set_Externe_SoundDatei)
you need to add: log("reloading: " & mod_allgemein.set_Externe_SoundDatei).

apologies to @aeric; i updated my response after your comment.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…