Android Question Get name of File from ContentChooser result?

Discussion in 'Android Questions' started by JohnC, Jul 28, 2019.

  1. JohnC

    JohnC Well-Known Member Licensed User

    I wish to obtain the filename of the selected file from the content chooser.

    For example, content chooser could return:

    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
    Directory = "ContentDir"
    Filename = "content://"

    I know that the filename that I chose is "ASLogo.jpg".

    And I see that there is a colon (%3A) right before the filename at the end of the string.

    So, my questions are:

    1) Is that last %3A guaranteed to ALWAYS be included in a content string and be the last delimiter before the filename?
    2) And if not, what is a good way to get the filename returned from CC no matter what format it returns?
  2. JohnC

    JohnC Well-Known Member Licensed User

    OK, I created this routine to extract the filename, but it is making a few assumptions:

    1) That real file paths will always start with "/"
    2) That content uri's will always have "%3A" at the end, before the file name.

    Sub GetFileNameFromContentResult(UriString As StringAs String
    If UriString.StartsWith("/"Then 'If the user used a file manager to choose file
            Return UriString.SubString2(UriString.LastIndexOf("/")+3,UriString.Length)
    else if UriString.StartsWith("content:"Then
    If UriString.IndexOf("%3A") > -1 Then
    Return UriString.SubString2(UriString.LastIndexOf("%3A")+3,UriString.Length)
    Else if UriString.IndexOf("/") > -1 Then
    Return UriString.SubString2(UriString.LastIndexOf("/")+3,UriString.Length)
    Return UriString
    End If
    Return UriString
    End If
    End Sub
    But, are these assumptions correct, or will there be exceptions?
    Last edited: Jul 29, 2019
  3. Erel

    Erel Administrator Staff Member Licensed User

    They are not correct.

    There is no reliable way to get the file name. Content providers are not expected to return the file name in the URI.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice