Android Question Very slow ExternalStorage.ListFiles

Pxs

Member
Licensed User
Hello

I'm trying to use the externalStorage library, everything works fine, but the ListFiles method is VERY slow on some phones.
With 60ish files it takes like about 4 seconds on a motorola g30. It's enough time that android pops up the message "The application is not responding, wait/kill?"

Taking a look around the forums, i tried replacing externalstorage.listfiles with:


B4X:
Public Sub ListFiles2 As List
    Dim folder As ExternalFile = Storage.Root   
    Dim files() As Object = folder.Native.RunMethod("listFiles", Null)
    Dim res As List
    res.Initialize
    
    For Each f As JavaObject In files
        If f.IsInitialized Then res.Add(Storage.FindFile(folder, f.RunMethod("getName", Null)))
    Next

    Return res
End Sub

Compiles fine, but i'm getting java.lang.NullPointerException: null receiver on the second line
Anyone knows why?



Also, for the second part seems like many posts refer to using :
B4X:
 DocumentFileToExternalFile(f)
instead of
B4X:
 ExtStorage.FindFile(folder, f.RunMethod("getName", Null))

but can't find anything about that method...


Thanks for your time
 

agraham

Expert
Licensed User
Longtime User
Storage Access Framework on Android, which underpins the ExternalStorage class, is slow, you can't avoid this. Also are the slow phones using slow SD Cards?

Look at the original code for ListFiles. I think you will find the slow part is

Folder.Native.RunMethod("listFiles", Null)

Even if your ListFiles2 worked it will be slower that the original ListFiles as you are invoking 'findFile' multiple times which searches the SD card whereas the original just picks objects from the existing List.

You will need to dig into the underlying Java to fully understand what is happening in the ExternalStorage clase - but be aware there are no short cuts round it.
 
Reactions: Pxs
Upvote 1

Pxs

Member
Licensed User
I see...

Well, hoping google improves the framework in the next version, i'll try to work around it by reducing the number of invocations.

I will try saving the list in KeyValueStore, and request a listfile invocation to update only if i suspect something changed, or by user request.

Fun fact: on 2 motorola g30, same model, no sd, same android version, similar number of files, there is a 40% difference in indexing time! Something weird is definitely going on here.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…