Android Question File.ListFiles not listing all the files

Jookus

Member
Licensed User
Longtime User
I need to access the files in the Download folder on my device. I am first requesting PERMISSION_WRITE_EXTERNAL_STORAGE and then listing the files in Download folder. For some reason not all are being listed. The code I use is like this

B4X:
Dim rp As RuntimePermissions
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
If Result = True Then
    Dim files As List = File.ListFiles(File.Combine(File.DirRootExternal, "Download"))
    For Each f In files
        Log(f)
    Next
End If

There are some pdf, binary and jpg files in the Download folder but I can get listed only the jpg files. If I use file manager app and change the name of the files that I can get listed and run the code again I can see the filenames being changed so I know I am working in the right folder. I already examined the file system read/write permissions (rwx), UID and GID but found no differencies between listed and unlisted files.
What could be the reason for this and how to fix it?

I have made a working solution with ExternalStorage but I would not like to use because it ruins the user experience. Specifically SelectDir dialog is annoying because if you would like to select the folder it opens in in you need first to go level up and the come back before you are able to make selection. Also it is confusing that you can see the files you would like access in the dialog but can't open them yet.
 

Jookus

Member
Licensed User
Longtime User
I haven't tried on a different device yet. I'll do that as soon as possible.

Yes, the target SDK is 29. I did not have that line in the manifest but now when I added it, I got a compiler fault saying
AndroidManifest.xml:18: error: attribute android:requestLegacyExternalStorage not found.

I copy/pasted the line from the post you linked and inserted in the manifest so it should not be due to a typo.

In that post it was also said that this workaround would not work in targetSdkVersion=30. So I removed the (non-functional) line from the manifest and changed targetSdkVersion=30 and ran the app again but with same results: some of the files in the folder are being listed, but not all of them.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Try targeting SDK 28 - that should work. I would have expected targeting SDK 30 to not work and produce an error, but possibly it is only showing you media files for some reason.

Targeting SDK 29 with that manifest entry should have worked. Have you got the latest SDK from here? If then Erel would need to comment on why it throws the error.
B4A – The simple way to develop native Android apps (b4x.com)

ExternalStorage should work with a single one-time selection by the user. I use it to access a load of mapping files in various folders on the SD card and apart from having to select the root of the SD card once after installation the users sees nothing. For an app in the Play Store this will soon be the only option to access Download.
 
Upvote 0

Jookus

Member
Licensed User
Longtime User
I didn't realize I had outdated SDK. I updated it and wow the above mentioned line in manifest works and all files are shown.

Thank you very much! I really appreciate your help.
 
Upvote 0
Top