Android Question SimpleExoPlayer URI Error - is there a regex way of replacing bad characters

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I got the following error using SimpleExoPlayer
androidx.media3.datasource.FileDataSource$FileDataSourceException: uri has query and/or fragment, which are not supported

The FileName in question has a # in it.
/storage/3864-6563/Music/Destiny's Child/#1's/03 - Survivor.mp3

Now this one is pretty simple for me to fix (either rename the file to remove the # or replaced the # with %23) but I am sure I have a lot more mp3 files with problems like this.

DOES Anyone have a URI Safe parser. Take a string and replace all the bad things with %xx

Seems like this could be done with Regex
Have this Regex string to tell me if there is something that needs to be replaced
[core]([^\x00-\x7F]|[&$\+,:;=\?@#\s<>\[\]\{\}|\\\^%])+[/code}

But have no idea on how to write a Regex string to replace the URI problem characters with valid ones.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Private Sub UrlEncodeAndKeepSlashes (url As String) As String
    Dim su As StringUtils
    Dim sb As StringBuilder
    sb.Initialize
    Dim i1 = url.IndexOf("/"), i2 As Int
    Do While i1 > -1
        If i1 > i2 Then
            sb.Append(su.EncodeUrl(url.SubString2(i2, i1), "utf8"))
        End If
        sb.Append("/")
        i2 = i1 + 1
        i1 = url.IndexOf2("/", i2)
    Loop
    If i2 < url.Length Then
        sb.Append(su.EncodeUrl(url.SubString2(i2, url.Length), "utf8"))
    End If
    Return sb.ToString
End Sub
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Will give that a try. tried to post yesterday that I had found this routine URI Decoding

Took the routine and removed the 2 Slash conversions and % conversion and flipped the target and replacement strings.



Tried your routine just now.
It is replacing spaces with + and doesn't seem to like it

File Name was
/storage/3864-6563/Music/Erasure/Erasure Pop! - The First 20 Hits/10 - A Little Respect.mp3

the replacement returned
/storage/3864-6563/Music/Erasure/Erasure+Pop!+-+The+First+20+Hits/10+-+A+Little+Respect.mp3

open failed: ENOENT (No such file or directory)]

Where my URI Decoding changes returns
/storage/3864-6563/Music/Erasure/Erasure%20Pop!%20-%20The%20First%2020%20Hits/10%20-%20A%20Little%20Respect.mp3

Which SimpleExoPlayer seems to like
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
For some reason every time I try to post the code inside the CODE blocks I get an error so attaching the code

Not sure I need all the replacements will find out after processing all the files

Thanks for helping
 

Attachments

  • StringToURI.txt
    954 bytes · Views: 22
Upvote 0
Top