Bug? File.ListFiles(File.DirAssets) different behaviour B4A / BaJ

klaus

Expert
Licensed User
There is a difference between B4A and B4J with File.ListFiles(File.DirAssets).

This code works in B4A but not in B4J:
For Each FileName As String In File.ListFiles(File.DirAssets)
This code works in B4J but not in B4A:
For Each FileName As String In File.ListFiles("../files/")

For cross-platform projects it would be interesting that the same codes works in all platforms.
I would prefer: For Each FileName As String In File.ListFiles(File.DirAssets)

I have not checked B4i.
 

Toky Olivier

Active Member
Licensed User
For cross-platform projects it would be interesting that the same codes works in all platforms.
I would prefer: For Each FileName As String In File.ListFiles(File.DirAssets)
Why not just use "#if B4a" and "#if B4J" directive because directory/file system are not the same? File.DirAssets in B4J doesn't sound so good even if it's the same folder name. It's just my opinion.
 

LucaMs

Expert
Licensed User
(A mad idea?)

Like XUI is the library that deals with "smoothing" the UI differences between the different platforms, for cases like this (others exist? To be investigated) a more general library (or more specialized libraries?) could be useful.

For example, even a "simple" b4xlib could expose an AssetsFiles property (List).
 

Toky Olivier

Active Member
Licensed User
I know this, but why having two different methods if I could hopefully have only one.
Indeed, It's easier and may be easy to include in XUI library like xui.DirAssets (as List).

May be later also (one more mad idea as @LucaMs said 😁) , one let us to use Inline Directives like in object pascal:
B4X:
For Each FileName As String In File.ListFiles({#If B4a}File.DirAssets{#Else}"../files/"{#EndIf})
So that you'll have just One line of code instead of 2 or more for example (how will be readability of the code???)
 

klaus

Expert
Licensed User
File.ListFiles doesn't officially support listing files from File.DirAssets.
But it does?
Does this mean that there is no 'official' way to get the list of the files in DirAssets in any of the three products?
But there do exist unoffical ways to do it, but different in two products.
I stumbled on this while I was trying to convert a project from Peter Simpson from B4A to B4J from this thread Creating long lists of data using xCLV (xCustomListView) with Lazy Loading.
 

William Lancee

Active Member
Licensed User
Although not officially supported, this works in both B4A and B4J

B4X:
Sub AssetsDir As String
#if B4J
    Return "../files/"
#else
    Return "AssetsDir"
#End If
End Sub
B4X:
    For Each FileName As String In File.ListFiles(AssetsDir)
        Log(FileName)
    Next
 

klaus

Expert
Licensed User
Yes, I know, it's the same as suggested in post #2!
It's a workaround, but it's not my 'wish'!
I am concerned with cross platform solutions.
Therefore in my mind, I should be able to use a same statement in all three products, without any conditional compiling, if it's technically possible.
My philosophy is: try to make the user's life easier despite your programmer life.
In our case, there are two possibilities: File.DirAssets or "../files/".
So, the system behind should handle this difference and not oblige the user to take care of it, well that's my point of view.
 
Last edited:

Filippo

Expert
Licensed User
So, the system behind should handle this difference and not oblige the user to take care of it, well that's my point of view.
This is what I expect from a cross-platform.
There are a lot of little things that need to be adjusted.
 

Erel

Administrator
Staff member
Licensed User
There is no bug here.

As the documentation in B4J says File.ListFiles doesn't support listing the assets folder.
The workaround suggested in the first post is wrong. It only seems to work because you are running the jar in the Objects folder. Once you move it to a different folder, it will fail.

This is what I expect from a cross-platform.
It is clearly documented that this feature is not supported in B4J, so naturally the behavior will not be the same. One path that will never be followed is removing or disabling features on one platform because they are not supported on a different platform.
 
Top