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

max123

Well-Known Member
Licensed User
Longtime User
I know that this is an old thread, but I concord that External Storage class in very slow comparated to old File api.
For this reason I have to modify my app to copy files from external storage to DirInternal before manage and read line by line.

I comparated it with old api directly on my app that I converted to work with new api, to list files it hangs a device with just 100 files it take about 15-20 seconds. With more files the app enter an indefinite state.

Not only ListFiles is like 10 times slow than old api, but every command is slow.

My app read gcode files and send over USB to a 3D printer, here I can see that it slow down a lot and 3D printer will wait some milliseconds the next gcode line. This absolutely do not happen on old api. For this reason I think I need to copy the file on DirInternal as explained before and next read from that position with old File api.

I will try these suggestions to speed up a bit ListFiles but for sure it cannot be so fast as old api.
 
Upvote 0
Top