Android Question List files of Main.rp.GetSafeDirDefaultExternal directory

DanteS

Member
Licensed User
Longtime User
Hi guys

To read the text of a .txt file in DefaultExternal and to read the list of all file in the directory I use the code shown below.

Why the File.ReadString instruction works fine and the File.ListFiles seams to read an empty list?

code:
    Dim x As String
    Dim ld As List
    ld.Initialize
           
    x = File.ReadString(Main.rp.GetSafeDirDefaultExternal(""), archi)
    ld=File.ListFiles(Main.rp.GetSafeDirDefaultExternal(""))

Note: archi is a variable containing the name of the file to be read
 
Last edited:

walt61

Well-Known Member
Licensed User
Longtime User
It works here - B4A 13.00, JDK 19, Android 12 (Xiaomi MIUI 14.0.6):
B4X:
    Dim rp As RuntimePermissions
    Log("Dir: " & rp.GetSafeDirDefaultExternal("")) ' Dir: /storage/emulated/0/Android/data/b4a.example/files
    File.WriteString(rp.GetSafeDirDefaultExternal(""), "test.txt", "Test")
    File.WriteString(rp.GetSafeDirDefaultExternal(""), "test2.txt", "Test2")
    File.WriteString(rp.GetSafeDirDefaultExternal(""), "test3.txt", "Test3")
    Log("Contents: " & File.ReadString(rp.GetSafeDirDefaultExternal(""), "test.txt")) ' Contents: Test
    Log("List: " & File.ListFiles(rp.GetSafeDirDefaultExternal(""))) ' List: (ArrayList) [test.txt, test2.txt, test3.txt]
 
Upvote 0

DanteS

Member
Licensed User
Longtime User
Hi Walt, thanks so much for your answer
Your code works fine, but I need to fill a spinner with the list of files, so I convert the text "List: (ArrayList) [file1, file2, ...]" into a list, with the subroutine named Split and then I fill the spinner co_archis with the subroutine fill_co, both subroutines are shown in the attached code.

Everything works fine, but I feel that it is too much code to fill the spinner. Maybe there is a way to fill the spinner directly with the string given by the instruction File.ListFiles(Main.rp.GetSafeDirDefaultExternal(""))
What do you think?


B4X:
Private Sub bt_getfiles_Click
    Dim ld As List
    Dim x As Object

    ld.Initialize

    x = File.ListFiles(Main.rp.GetSafeDirDefaultExternal(""))

    ld=Split(x)
    Fill_co(co_archis, ld)
End Sub


Sub Fill_co(co As Spinner, l As List)
    If co.Size > 0 Then
        co.Clear
    End If

    For i=1 To  l.Size
        co.Add(l.get(i-1))
    Next   
End Sub


Sub Split(linea As String) As List
    Dim l As List
    Dim x As String
    Dim a, b As Int

    l.Initialize

    b = linea.IndexOf("[")

    If b > -1 Then
        linea = linea.SubString2(b + 1, linea.Length)
        b = linea.IndexOf("]")

        If b > -1 Then
            linea = linea.SubString2(0, b)
        End If
    End If

    b = 0

    Do Until(b = -1)
        b = linea.IndexOf(",")

        If b > 0 Then
            x = linea.SubString2(0, b)
            linea = linea.SubString2(b + 1, linea.Length)
        Else
            x = linea
        End If

        l.Add(x)
    Loop

    Return l
End Sub
 
Upvote 0

walt61

Well-Known Member
Licensed User
Longtime User
Wow you missed something here :) The reason you see "(ArrayList) [test.txt, test2.txt, test3.txt]" is that this is a list that's being logged (i.e. converted to a string representation). File.ListFiles returns a list, so all you need to do is:
B4X:
co_archis.Clear
co_archis.AddAll(File.ListFiles(rp.GetSafeDirDefaultExternal("")))
 
Upvote 0
Top