[Libgdx] Asset file availability on file system?

Doing some work on my JFreeType lib. Juts managed to compile my natives successfully on Android (At last…).

Now the problem is that when I do

String path = Gdx.files.internal("Tahoma-Bold.ttf").file().getAbsolutePath();

path is equals to:

"/Tahoma-Bold.ttf"

When running the same code on Windows, I get this path:

"C:\Users\Work\Desktop\libgdx workspace\PlaneSimWorkspace\PlaneSim\android\assets\Tahoma-Bold.ttf"

Does this mean that on Android gdx doesn’t have files available on files system or something? I have no idea how to describe it, but I think you get the point. The path on android should be like:

"/android/apps/MyApp/Assets/Tahoma-Bold.ttf"

Gdx.files.internal() refers to the working directory of the application.

On Android the working directory is the assets folder. On desktop it is where your application is ran from/compiled from whatever.

Also you can’t access any apps “assets” or working directory on Android, not even with a file manager and rooted device, not from what I can see anyway.

This isn’t really a problem for me to handle, but it is painful that Gdx didn’t handle that for me.

I can just read the file and save it where ever I want. It doesn’t really matter since I only need this for fonts, which are only a few.

You should probably have a look at the wiki page for file handling. It has a very good explanation what file “locations” exist (internal/local etc.) and what they are used for: https://github.com/libgdx/libgdx/wiki/File-handling

Thing is that android (and also the webgl export) expose the underlying filesystem a bit differently with different capabilities. It is essential to understand this if you plan to deploy to different targets than the desktop. It can also be very useful for desktop.

On android you have your files usually in your jar (if you used the gdx setup ui it should be already setup correctly). If you use Gdx.files.internal it will loom in the jar for your files.

That is a problem, because file needs to be available on the file system for FreeType to load the font - I need to specify file name. (I think thats how it works)

Why is that a problem. Put the files into your android assets folder and it will be available via Gdx.files.internal in your desktop, android and web version…

Please read the original post.

I have read the original post. And I don’t see a problem only a bit confusion from your side. Probably you need to explain your question better.

  1. Do you only need to read the files?
  2. Do you have the files at compile time?

If your answer to both questions is yes, then use the android assets folder and load via internal as I said. Android has a different folder structure so the path will look different thn on your desktop, but when only reading it should behave the same…

If that doesn’t help you then please extend your question with some more info on what you are trying to achieve.


String path = Gdx.files.internal("Tahoma-Bold.ttf").file().getAbsolutePath();
File file = new File(path);
file.exists(); // This has to return true. Otherwise it means that file doesn't exist.

On Windows file exists, on Android file doesn’t exist.

Gdx.files.internal already returns a file handle. I don’ see why you get the absolute path and then a new ile handle from it. As I mentioned android behaves a bit differently in regards to the file system. Getabsolutepath might not be supported on android. But with the example you have given you don’t neesd it.

Sorry, I’m at work and writing from my phone. I can probably setup a small example project when I’m home.

Atombrot allready told you, the internal() FileHandle reffers to a path, relative to the applications asset folder and this is somehow “locked”, as Gibbo3771 mentioned.
Also, this FileHandle can only be used to read files, not to write.
If you want to access the “normal” file system, you can use either the local() FileHandle, the external() FileHandle, relative to the SD-Cards root or the absolute(), where you can give the absolute path.

Also note, that the FileHandle allready has a method exists(), so your code could be much smaller+simpler:

Gdx.files.internal("Tahoma-Bold.ttf").exists()

You don’t need to do any of that.

Gdx.files.internal() returns the file in question. Just do:

File file = Gdx.files.internal().file()

LibGDX will solve the rest.

You guyz don’t understand my problem. I need the file path. The file path has to be valid. You can check if it is valid by doing new File(path).exists().

I need a valid file path for FreeType. FreeType is a native library. It loads font files. It doesn’t accept java.File. It just wants a file path from which to load a font. When I pass any of the values you guys suggested, it doesn’t load my font, because the file doesn’t exist!

Maybe we don’t understand your question because you add more important (and previously unkown)information with every post, which should have already been stated in the opening post. Keep in mind that we don’t have more context than you provided us and things that seem obvious to you don’t have to be obvious for us.

If it is a native library and the library does the loading of the file itself it will most likely not work. check the documentation of the lib to see if android is supported. If so you should be able to find hints how to supply the correct path. So the library would be my first place to look for.

Ah we dont need the context really…

you need the absolute path for the file, gotcha (technically you should never need that… I think…, but if thats what you want, fine)

Gdx.files.internal("Tahoma-Bold.ttf").file().getAbsolutePath();

ok so file() gives you a java File class. I do recall that if you convert the libgdx FileHandle to File like that on android there are indeed compatibility issues.
Just read the documentation thoroughly for this

I have long since solved my issues.

I just read the file in java, and write it where ever I want. Then I can access it like I need to.

Also:


Gdx.files.internal("Tahoma-Bold.ttf").path();