9
\$\begingroup\$

When running or debugging my game on Android device, I get this error in the logcat:

com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: data/fonts/myFont.ttf (Internal)

Which is created by this code line:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);

where fontFile is defined like this:

FileHandle fontFile = Gdx.files.internal("data/fonts/myFont.ttf");

This doesn't happen when I run the desktop configuration.

I know that for desktop configuration you have to define the working directory of your project, but I don't see such option in android configuration and it also makes no sense, so I don't think it's a "working directory"-related problem.

Obviously, the file is in the correct path.

Also note that everything worked fine in my previous project. I created this new project by creating a new blank prj using libgdx-setup and then copying all the classes and packages from the older project. So maybe it's a problem related to some Gradle file?

\$\endgroup\$
  • 1
    \$\begingroup\$ are you getting a FileNotFoundException? Android file-system is case sensitive, make sure that you wrote it correctly "myFont.ttf" is different that "myFont.TTF" \$\endgroup\$ – Alex Sifuentes Jul 6 '15 at 13:36
  • \$\begingroup\$ Yes, i didn't read it in the logcat, here is the other part of the logcat message: Caused by: java.io.FileNotFoundException: data/fonts/myFont.ttf at android.content.res.AssetManager.openAsset(Native Method) at android.content.res.AssetManager.open(AssetManager.java:359) at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75) I'm sure file is correct, it's inside assets/data/fonts/myFont.ttf I think for some reason Android is not looking in the correct folder... But running the desktop configuration works fine \$\endgroup\$ – Davide Cattani Jul 6 '15 at 17:53
0
\$\begingroup\$

I believe for android you need to have an assets folder with all of the assets loaded into it, this assets folder would be in the android project rather than the core project. Desktop, HTML, etc all use core project assets so they would work fine, but android needs its own assets folder. do you have one for it?

\$\endgroup\$
  • 2
    \$\begingroup\$ Desktop HTML and core use Android assets folder, which is the only assets folder in the project. \$\endgroup\$ – Davide Cattani Jul 12 '15 at 10:46
0
\$\begingroup\$

The Options are:

1) Try TTF upper case instead of ttf in all of your code - in case your original file has TTF suffix, because android is case sensitive.

2) In build.gradle file under your android module make sure that:

assets.srcDirs = ['assets']

And not:

assets.srcDirs = ['assetManager']

In another words make sure its your android assets folder. this one worked for me. then clean build project, and uninstall old app from android device.

\$\endgroup\$
  • \$\begingroup\$ [DMGregory] got it, answer edited. \$\endgroup\$ – Yan.F May 31 '17 at 19:58
-1
\$\begingroup\$

Try copying the font files directly into the assets folder.

\$\endgroup\$
-1
\$\begingroup\$

Just do one thing you can change the method which your following now. you can use the "hiero" tool for the font creation . Just download "hiero" jar file from the following link

https://libgdx.badlogicgames.com/tools.html

by using hiero you will be able to access almost all the fonts . after creating the fonts save it on your android project files "asset" folder and add the folowing code.

     private BitmapFont font;
     font = new BitmapFont(Gdx.files.internal("myNewFontCreatedByHiero.fnt"));

just try this method. it will work.

\$\endgroup\$
  • \$\begingroup\$ Sorry, have to downvote this. Using the FreeTypeFontGenerator let's you import any font and write with it in any size. Where Hiero requires you to generate bitmaps for each font and size. I agree that hiero is a great tool but this is not answering the question. \$\endgroup\$ – Madmenyo Jan 19 '17 at 6:36
  • \$\begingroup\$ @Madmenyo Hi, I just give a alternate way to solve his problem. The above concept is not wrong. Which is the alternate solution to his problem. I don't know what is the exact reason for down vote. \$\endgroup\$ – Gokul Sreenivasan Jan 19 '17 at 7:22
  • \$\begingroup\$ How do you know? Maybe he needs a lot of dynamically generated text, hiero does not help with that. \$\endgroup\$ – Madmenyo Jan 19 '17 at 7:39
  • \$\begingroup\$ @Madmenyo Hi , you may read his question first. I have given an alternat solution. \$\endgroup\$ – Gokul Sreenivasan Jan 19 '17 at 7:41
  • \$\begingroup\$ He wants to load ttf you suggest creating a bitmap font. \$\endgroup\$ – Madmenyo Jan 19 '17 at 7:43
-1
\$\begingroup\$

Make sure the files are inside /android/assets.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.