How can I list the files in a folder "books" that I have created inside the assets folder?
I tried:
B4X:
Dim mylist As List = File.ListFiles(File.DirAssets & "books")
But Error:
B4X:
java.io.IOException: AssetsDirbooks is not a folder.
at anywheresoftware.b4a.objects.streams.File.ListFiles(File.java:158)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at JHS.SkyTest.main.afterFirstLayout(main.java:102)
at JHS.SkyTest.main.access$000(main.java:17)
at JHS.SkyTest.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Maybe the Book folder is not there. If you want to check if it is present, try this
B4X:
Try
Dim mylist As List = File.ListFiles(File.DirAssets)
Dim B As Boolean = False
For Each nf As String In mylist
If nf.IndexOf("book")>-1 Then B=True
Next
Log(B)
Catch
Log(LastException.Message)
End Try
Maybe the Book folder is not there. If you want to check if it is present, try this
B4X:
Try
Dim mylist As List = File.ListFiles(File.DirAssets)
Dim B As Boolean = False
For Each nf As String In mylist
If nf.IndexOf("book")>-1 Then B=True
Next
Log(B)
Catch
Log(LastException.Message)
End Try
The ListFile command could return the file name along with the entire path, or with an extension, so it may not be recognized if you only search for the word "book", so you must check with IndexOf
Using the code below, ListAllFiles does not recognise any subfolder or list files inside it. Guess you'll need to "flatten" your folder structure (by putting all files in the main Files folder but renaming to replace "/" with "$" or similar in the full path name) then recreate the structure in File.DirInternal on App startup.
B4X:
Sub Activity_Create(FirstTime As Boolean)
ListAllFiles(File.DirAssets)
Activity.Finish
End Sub
Sub ListAllFiles(sFolder As String)
Dim oList As List = File.ListFiles(sFolder)
For Each sFile In oList
Log(File.Combine(sFolder, sFile))
If File.IsDirectory(sFolder, sFile) Then ListAllFiles(File.Combine(sFolder, sFile))
Next
End Sub