1

I saved in the resource my font files *.ttf and I want to get it back when my application was started but I couldn't do this. I have the following code:

final URL fontUrl = getClass().getClassLoader().getResource("/fonts/" + font);
                if (fontUrl == null) {
                    continue;
                }
                File fontFile = new File(fontUrl.getFile());
System.out.println(fontFile.exists());    // return false
System.out.println(fontFile.length());    // return 0
return fontFile;

How can I get a normal file from resource?

John
  • 1,375
  • 4
  • 17
  • 40

1 Answers1

1

Have you placed in it assets folder? If you are looking forward to use it in TextView etc,:

  Typeface typeface = Utils.getTypeface(context, textStyle);
    if (typeface != null) setTypeface(typeface);

and the function in Utils file is:

public static Typeface getTypeface(Context context, int textStyle) {
    try {
        String font = "fonts/font-file-name.ttf"
        Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), font);
        return myTypeface;
    } catch (Exception e) {
        Log.d(Constants.TAG, "OpenSansTextView init: ");
        e.printStackTrace();
    }
    return null;
}

The Location of my ttf file:

app\src\main\assets\fonts\font-file-name.ttf
Kishita Variya
  • 810
  • 9
  • 19