Android Question [SOLVED] User selects File to import - with Filename

lip

Active Member
Licensed User
Longtime User
The user import bitmaps which have been sent to the tablet - normally by downloading from an email. The bitmaps are stored in an internal folder and the Filename stored in a database, so that the correct bitmap can be opened and displayed when required.

Since recent Android updates our Apps can no longer access the tablet's folders directly. I'm using FileHandler to use the tablet's file browser to select the file. This returns Result.Filename and Result.Realname. Most of the time Result.Realname gives me the correct filename. However, with Android 13 it depends on how the user selects the file: If they select it from a list then I get the correct filename, but if they select it from a page of icons then Result.Realname is just a random integer eg 103276337637.png. Result.Filename is the same integer, but appended to the end of the path.

The most promising work around is this one Getting file info using uri but it looks complicated.

In this post Get name of file from content chooser there is a quote from our leader saying "There is no reliable way to get the file name. Content providers are not expected to return the file name in the URI."

Before I get bogged down, is there a simple workaround? Or am I wasting my time trying?
 
Solution
Try this

B4A:
Dim CC As ContentChooser
    CC.Initialize("cc")
    CC.Show("*/*", "Choose file")
    Wait For CC_Result(Success As Boolean, Dir As String, FileName As String)
    
    If Success Then
        
        Dim mFileName As String
        mFileName = GetFileInfoByIndex("_display_name", FileName)
        ...
        ...
    End if   
    
    Sub GetFileInfoByIndex(column As String, uri As String) As String
    Dim results As String
    Dim Cur As Cursor
    Dim Uri1 As Uri
    Dim cr As ContentResolver
    cr.Initialize("")
    If uri.StartsWith("content://media/") Then
        Dim i As Int = uri.LastIndexOf("/")
        Dim id As String = uri.SubString(i + 1)
        Uri1.Parse(uri)
        Cur = cr.Query(Uri1, Null, "_id =...

zed

Active Member
Licensed User
Try this

B4A:
Dim CC As ContentChooser
    CC.Initialize("cc")
    CC.Show("*/*", "Choose file")
    Wait For CC_Result(Success As Boolean, Dir As String, FileName As String)
    
    If Success Then
        
        Dim mFileName As String
        mFileName = GetFileInfoByIndex("_display_name", FileName)
        ...
        ...
    End if   
    
    Sub GetFileInfoByIndex(column As String, uri As String) As String
    Dim results As String
    Dim Cur As Cursor
    Dim Uri1 As Uri
    Dim cr As ContentResolver
    cr.Initialize("")
    If uri.StartsWith("content://media/") Then
        Dim i As Int = uri.LastIndexOf("/")
        Dim id As String = uri.SubString(i + 1)
        Uri1.Parse(uri)
        Cur = cr.Query(Uri1, Null, "_id = ?", Array As String(id), Null)
        Cur.Position = 0
        If Cur.RowCount <> 0 Then
            For i = 0 To Cur.ColumnCount - 1
                If Cur.GetColumnName(i) <> Null Then
                    If Cur.GetColumnName(i) = column Then
                        results = Cur.GetString2(i)
                        Exit
                    End If
                End If
            Next
        End If
    Else
        Uri1.Parse(uri)
        Cur = cr.Query(Uri1, Null, Null, Null, Null)
        Cur.Position = 0
        If Cur.RowCount <> 0 Then
            For i = 0 To Cur.ColumnCount - 1
                If Cur.GetColumnName(i) <> Null Then
                    If Cur.GetColumnName(i) = column Then
                        results = Cur.GetString2(i)
                        Exit
                    End If
                End If
            Next
        End If
    End If
    Cur.Close
    Return results
End Sub
 
  • Like
Reactions: lip
Upvote 0
Solution
Top