Android Question [SOLVED] Problem using File.ListFiles on Download folder - did something change?

Andris

Active Member
Licensed User
Longtime User
Up until recently I've been using File.ListFiles to list the contents of the Download folder and have had no problem. This is basically the code I've used (and yes, the permissions have been granted!):
B4X:
    Dim l As List = File.ListFiles(File.Combine(File.DirRootExternal,"Download"))
    If l.IsInitialized And l.Size > 0 Then
        For i=0 To l.Size-1
            Log(l.Get(i))
        Next
    End If

There are KML and KMZ files in my Download folder, which my app then imports. Now, for a reason I can't figure out, only media files appear (JPG and PNG for example), and the KML/KMZ ones don't get listed! What could be causing File.ListFiles to exclude them?
 

agraham

Expert
Licensed User
Longtime User
You can't do that in API 29. Read this for the workaround or target API 28 when it will still work.

 
Upvote 0

Andris

Active Member
Licensed User
Longtime User
You can't do that in API 29. Read this for the workaround or target API 28 when it will still work.


Thanks @agraham. I found this just as you answered!

In summary, for now, add this to manifest editor:
B4X:
SetApplicationAttribute(android:requestLegacyExternalStorage, true)
 
Upvote 0

johnmie

Active Member
Licensed User
Longtime User
this will be my last question (for this year).
I tired it but got this manifest error:
Packaging files. Error
AndroidManifest.xml:22: error: No resource identifier found for attribute 'requestLegacyExternalStorage' in package 'android'


In my code users receive sometext.csv as email attachment and are asked to simply save this, which puts it into the download folder to be picked up by my app.
Without that line in manifest I can see that the file exists but when trying to list all files, the fileList throws an error.

B4X:
Dim A As String = "/storage/emulated/0/Download"
    Dim B = Starter.rp.GetSafeDirDefaultExternal("Download")
    Log("Dir A, B = "&A&" >> "&B)
    
    If File.Exists(A,"sometext.csv") = True Then
        Log("FOUND IT")
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE)
        Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
        If Result=True Then
            Log("Permission granted")
        End If
      Try   
        Dim fileList As List
        fileList.Initialize
        Log("Show all in "&A)
        fileList = File.ListFiles(A)
        For i = 0 To fileList.Size-1
            Log(fileList.Get(i))
        Next
      Catch
        Log("Error = "&LastException.message) ' = java.lang.RuntimeException: Object should first be initialized (List).
      End Try
    Else
        Log("you goofed it up again")
    End If

Does anyone know what I've done wrong?

Thanks for your help and happy beforexing in 2021!
john m.
 
Upvote 0
Top