Android Question ExternalStorage seems to be very slow

CaptKronos

Active Member
Licensed User
I have just started using the ExternalStorage class and have noticed it is very slow. Or am I doing something wrong?
The following line takes about 45s to execute on a Samsung Galaxy S8. (anExternalFile points to a folder with 1000 files.)
B4X:
theFilesList=Storage.ListFiles(anExternalFile)
And the following takes another 45s to execute.
B4X:
For Each aFile As ExternalFile In theFilesList
  If aFile.isfolder=False Then       
  End If
Next
Is this to be expected? At least with the second example, I can add some sleep(0) and show a progress bar, but with the first example, the GUI locks up.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I mean: On the phone it uses the default application for file management and performs the same operation
This is a completely different API.

Use ExternalStorage class.
Change ListFiles to:
B4X:
Public Sub ListFiles (Folder As ExternalFile) As List
  Dim n As Long = DateTime.Now
    Dim files() As Object = Folder.Native.RunMethod("listFiles", Null)
  Log(DateTime.Now - n)
    Dim res As List
    res.Initialize
  n = DateTime.Now
    For Each f As JavaObject In files
        res.Add(DocumentFileToExternalFile(f))
    Next
 Log(DateTime.Now - n)
    Return res
End Sub

My guess that the slow part will be the second one. You can modify the code and only get the file name. Maybe it will be faster.
 
Upvote 0

CaptKronos

Active Member
Licensed User
Also, just to add after some further testing, returning only the file names takes just under half the time compared to returning all the fields.
 
Upvote 0
Top