Android Question File.IsDirectory fails

ilan

Expert
Licensed User
Longtime User
hi

i am trying to check if a file i get from File.ListFiles(...) is a directory but i am getting a crash.

i am targeting sdk26 and i request the permission to _WRITE_EXTERNAL_STORAGE so that should not be the problem.

this is my code:

B4X:
Dim list1  As List
list1.Initialize
list1 = File.ListFiles(File.DirRootExternal)

Log("size: " & list1.Size)

For x = list1.Size-1 To 0 Step -1
    If File.IsDirectory(File.DirRootExternal, "/" & list1.Get(x)) = False  Then
        list1.RemoveAt(x)
    End If
Next

i also tried to remove "/" before the file name but did not worked.

this is the crash log:

SENTINEL_MSG_LIBCUTILS
SENTINEL_MSG_LIBLOG
size: 92
menu$ResumableSub_phoneout_Clickresume (java line: 23594)
java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at anywheresoftware.b4a.objects.collections.List.RemoveAt(List.java:104)
at www.sagitalcashlite.net.menu$ResumableSub_phoneout_Click.resume(menu.java:23594)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

any idea what is wrong?

i also need to mention that some files are written in hebrew, could that be the reason?

thanx, ilan
 

ilan

Expert
Licensed User
Longtime User
ok, ok, ok, i got it :oops:

listfiles.png


i just create now a second list and copy all directories to it and use it instead of list1 :)

B4X:
Dim list1, list2 As List
list1.Initialize
list2.Initialize

list1 = File.ListFiles(File.DirRootExternal)

For x = 0 To list1.Size-1
    If File.IsDirectory(File.DirRootExternal, "/" & list1.Get(x)) Then
        list2.Add(list1.Get(x))
    End If
Next
 
Upvote 0
Top