Android Question Access to files in b4a 10.70 BETA # 2

coldteam

Active Member
Licensed User
Longtime User
Trying a new beta b4a and a question arose.
I use LibGdx in my projects, and I used to store files like this: (This is just an example)
Files/
npc/goblin.png
monsters/goblin.png
player/player1.png
player/player2.png
I have stored files in different folders and downloaded them asynchronously via asset manager.
I have not used the File Manager built into b4a in any way.
In the new beta version, I get the error, the file cannot be downloaded with this address. As in the screenshot.
When I add a file through the File Manager, it is located in the root directory of the project's Files folder.
If I understand correctly, now it will not work to use folders for this, and you just need to divide files into groups, but only in the built-in File Manager B4A.

As a result, there cannot be 2 files with the same name in one project. Correct me, maybe I didn't understand how it works.

The main question is how to access files in subfolders in the new version 10.70.
 

Attachments

  • 321123.jpg
    321123.jpg
    510.1 KB · Views: 200
Last edited:

coldteam

Active Member
Licensed User
Longtime User
Please post logs as text instead of screenshot.

I'm unable to reproduce it.
This code works:
B4X:
Log(File.ReadString(File.DirAssets, "test\1.txt"))

I think I found a problem, please check it

I have now run tests at 10.60 and 10.70
with your example
B4X:
Log(File.ReadString(File.DirAssets, "test\1.txt"))

at 10.60 I get an error if I use your code. At 10.70 it works. The point is in the slash, in which direction it is directed.
B4X:
java.io.FileNotFoundException: test\1.txt

At 10.60 the code works:

B4X:
Log(File.ReadString(File.DirAssets, "test/1.txt"))

and I've always used this slash "/" apparently as inside the libgdx library. therefore, in the new version, the asset manager does not see the file, no matter how I put a slash.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are correct. The slash direction changed, however it happens in the underlying packaging tool and this behavior cannot be modified.

I've changed the B4A framework code, so this line will work:
B4X:
Log(File.ReadString(File.DirAssets, "test/1.txt"))

A similar change needs to be done in libgdx library as it loads the assets directly.
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
You are correct. The slash direction changed, however it happens in the underlying packaging tool and this behavior cannot be modified.

I've changed the B4A framework code, so this line will work:
B4X:
Log(File.ReadString(File.DirAssets, "test/1.txt"))

A similar change needs to be done in libgdx library as it loads the assets directly.

will this change take effect with the release of this version or will it be BETA # 3?
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
I'll probably release another beta.

As I wrote, it will not affect the behavior of libgdx which loads the files directly.

Yes, I understand. I wrote this information to @Informatix and I hope he will find the time.
Thanks for answering this. So Google made changes to the sdk? But why ... Many libraries will require a rework.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
I tried beta 3 today and the compilation failed because my version of Java is too old, so I downloaded the JDK 11 from this site and unzipped it to my hard drive. Unfortunately the compilation with B4A 10.6 or 10.7 and this JDK failed:
Génération du fichier R. (0.29s)
Compilation du code Java. (1.93s)
Conversion bytes code - dex optimisé. Error
I did not change anything to the default configuration for the Dexer part.
Unfortunately, I cannot replace the Oracle Java version due to other requirements and I don't have time to spend on this subject as I'm very busy with non-Android projects.
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
I started looking and found that it is no longer possible to know whether an item in the assets is a directory or a file, whether it exists or not. So I have a big problem at the moment to fix libGDX.

Maybe this will help?
B4X:
public boolean exists () {
  if (type == FileType.Internal) {
    String fileName = file.getPath();
    try {
      assets.open(fileName).close(); // Check if file exists.
      return true;
    } catch (Exception ex) {
      // This is SUPER slow! but we need it for directories.
      try {
        return assets.list(fileName).length > 0;
      } catch (Exception ignored) {
      }
      return false;
    }
  }
  return super.exists();
}

If not, and I didn't fully understand the problem. Is it possible to bypass it. As I understand it, this function is used to avoid 2 downloads of 1 file? (In cases like this)
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Maybe this will help?
B4X:
public boolean exists () {
  if (type == FileType.Internal) {
    String fileName = file.getPath();
    try {
      assets.open(fileName).close(); // Check if file exists.
      return true;
    } catch (Exception ex) {
      // This is SUPER slow! but we need it for directories.
      try {
        return assets.list(fileName).length > 0;
      } catch (Exception ignored) {
      }
      return false;
    }
  }
  return super.exists();
}

If not, and I didn't fully understand the problem. Is it possible to bypass it. As I understand it, this function is used to avoid 2 downloads of 1 file? (In cases like this)
This is one of the functions that no longer works because assets.list always returns an empty list (IsDirectory does not work either, for the same reason).
For assets.open to work, just replace the / with \, but there are problems with some files (e.g. music and sounds in my first tests).
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
This is one of the functions that no longer works because assets.list always returns an empty list (IsDirectory does not work either, for the same reason).
For assets.open to work, just replace the / with \, but there are problems with some files (e.g. music and sounds in my first tests).

This is how the function looks now in the original library on github. Last changes from August 2020.
B4X:
    public boolean exists () {
        switch (type) {
        case Internal:
            if (file().exists()) return true;
            // Fall through.
        case Classpath:
            return FileHandle.class.getResource("/" + file.getPath().replace('\\', '/')) != null;
        }
        return file().exists();
    }

and if you do that exists in the B4A library will always return true. When are errors possible? or are other assets functions not working correctly too?
 
Upvote 0

coldteam

Active Member
Licensed User
Longtime User
The code is in AndroidFileHandle.java.
It seems that the new B4A version is breaking something because the AssetManager list function should work; it has not become obsolete and I did not find mention of a malfunction on the internet.

Can only @Erel help us with this?
This code works fine in Android Studio as I see it.

What features of b4a have influenced the behavior of this class? Any ideas to make the search easier.
 
Upvote 0
Top