Android Question List files in DirAssets

Declan

Well-Known Member
Licensed User
Longtime User
Looking at this post:
This is apparent:
File.ListFiles doesn't work with subfolders inside the assets folder.
How can I list the files in a folder "books" that I have created inside the assets folder?
I tried:
Code:
Dim mylist As List = File.ListFiles(File.DirAssets & "books")
But Error:
Code:
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)
 
AssetsDirbooks????

INSERT a / or use file.combine...
 
Upvote 0
Tried:
Code:
Dim mylist As List = File.ListFiles(File.DirAssets & "/books")
and:
Code:
Dim mylist As List = File.ListFiles(File.Combine(File.DirAssets, "books"))
Same error
 
Upvote 0
you cannot access the folders in dirassets, try copying the Files in DirRootExternal or DirDefaultExternal
 
Upvote 0
Maybe the Book folder is not there. If you want to check if it is present, try this

Code:
    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
You can only access DirAssest to read
 
Upvote 0
try this .. see if the folder(books) will not appear in the logs
Code:
Dim list As List
    list = File.ListFiles(File.DirAssets)
    For i = 0 To list.Size - 1
        Log(list.Get(i))
    Next
 
Last edited:
Upvote 0
try this .. see if the folder(books) will not appear in the logs
Code:
Dim list As List
    list = File.ListFiles(File.DirAssets)
    For i = 0 To list.Size - 1
        Log(list.Get(i))
    Next

Folder books does appear in the logs:
Code:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
in wrapper and event name  = sk
.DS_Store
books
fonts
images
library_black.png
library_white.png
main.bal
sql
_assets_map
 
Upvote 0
Maybe the Book folder is not there. If you want to check if it is present, try this

Code:
    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
You can only access DirAssest to read
B == true (so folder "books" is present) but how to list the files in the folder.
 
Upvote 0
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 ;)
 
Last edited:
Upvote 0
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.

Code:
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
 
Upvote 0
Back
Top