"Native typeface cannot be made" only for some people

59,620

Solution 1

This bug of Android OS could be the reason of your issue:

Typeface.createFromAsset leaks asset stream

Where are also a workaround in this bugreport:

I altered HTH's workaround so that the method does not assume the font path or format. The full path of the font asset must be submitted as a parameter. I also wrapped the call to createFromAsset() in a try-catch block so that the get() method will return null if the asset is not found.

public class Typefaces {
    private static final String TAG = "Typefaces";

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c, String assetPath) {
        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface t = Typeface.createFromAsset(c.getAssets(),
                            assetPath);
                    cache.put(assetPath, t);
                } catch (Exception e) {
                    Log.e(TAG, "Could not get typeface '" + assetPath
                            + "' because " + e.getMessage());
                    return null;
                }
            }
            return cache.get(assetPath);
        }
    }
}

Solution 2

I followed some of the solutions found here, with no success. I thought it was something really obscure, as programmers often do. Then somewhere I read it could be related to the font path, gotcha:

Instead of:

Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
                                             "blanch_caps.ttf");   

I changed to:

Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
                                             "fonts/blanch_caps.ttf");   

And my file is in assets/fonts/blanch_caps.ttf. Not it works like a charm!

Solution 3

This error came up when the font was in the library asset folder. When I copied it into assets of the application which was using this library, the error disappeared.

It seems assets cannot be imported: Android Library assets folder doesn't get copied

And here are some other cases: Issue when using a custom font - "native typeface cannot be made"

Solution 4

I was struggling with this a lot. I tried every possibility and nothing helps. In the end, to problem was somewhere else. If you are building your project with Gradle, don't forget to add these lines in build.gradle file. This solved the problem in my case.

    sourceSets {
    main {
        assets.srcDirs = ['assets']
    }
}

Solution 5

You must create assets folder inside src-->main in AndroidStudio. This way worked!

Share:
59,620
user936580
Author by

user936580

Updated on July 05, 2022

Comments

  • user936580
    user936580 almost 2 years

    I have an app that changes the font typeface for some elements. It works well for most of the people, but maybe a 0.5% get an exception when trying to change the font. The significant part of the stack trace is this:

    Caused by: java.lang.RuntimeException: native typeface cannot be made
    at android.graphics.Typeface.<init>(Typeface.java:147)
    at android.graphics.Typeface.createFromAsset(Typeface.java:121)
    

    As I say, it works for most of the people, so I don't think it is a problem with the font file or my code. Any suggestions about how to solve this?

    Edit: This is my code:

    Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
                                                     "fonts/CharisSILR.ttf");
    TextView tv;
    tv = ((TextView) findViewById(R.id.searchPronunciationTitle));
    tv.setTypeface(phoneticFont);
    
  • user936580
    user936580 over 11 years
    Thank you. If I understand it correctly, that bug's description says that the problem is a memory leak that will provoke out of memory bugs. However, this is not what I seeing: I get the "native typeface cannot be made" message without out of memory exceptions. In addition, it looks that it always fails for the user that fail, and my application doesn't use many resources.
  • user936580
    user936580 over 11 years
    Thank you, but I have the font in the application asset folder. It works in most of the occasions, but for some people it doesn't work, I don't know why.
  • Lumis
    Lumis over 11 years
    Good to know. I have placed try/catch around loading every custom font, if error it will default to the inbuilt android font, in case a ttf font does not work on some devices.
  • user936580
    user936580 almost 11 years
    Thank you for the answer. I'm using Eclipse, so that is not my problem.
  • Bolhoso
    Bolhoso over 10 years
    @user936580 the catch block when loading the typeface will avoid the exception to explode to your user. The trade-off here is an ugly font instead of a crash. I think it pays :)
  • Compaq LE2202x
    Compaq LE2202x over 10 years
    @Hit How do you use/call this method? I've been trying to figure out.
  • Mr. B
    Mr. B about 10 years
    if we are using Android studio and gradle - Watch this. The assets dir should be directly under main. This resolved my issue. Didn't have any problem in font name and font file. This is especially important as the font name that we were using had upper/lowercases whereas asset file name should be all lowercase.
  • ylka
    ylka about 10 years
    this did work for me, thanks a lot! although a little tip: my font was called with capital letters, like "COOPBL.TTF" so I had to write it in the code also with capital letters.. maybe it helps someone :)
  • Dominic K
    Dominic K almost 10 years
    Under Android Studio (0.8.1 currently), I didn't have to add the lines to build.gradle, but I did have to put my assets folder directly under main (eg. src\main\assets)
  • CodyMace
    CodyMace almost 10 years
    DMan's solution was perfect for me. I didn't need to do anything in build.gradle
  • Michal
    Michal almost 10 years
    This answer was valid at that time when Gradle Android Plugin was below 0.5 and you needed to specify res folder by yourself. Now it's not necessary of course!
  • Saeed.re
    Saeed.re almost 10 years
    I think it's because you use your application's context for getAssets() not library ... +1 worked for me .
  • Amin Keshavarzian
    Amin Keshavarzian over 9 years
    Don't add this bit of cod if you're using Android Studio 8.1 and above, I removed these lines and it works ! this code is for earlier versions of Android Studio
  • Dirk
    Dirk over 9 years
    I didn't help me to get rid of the Exception, BUT it stops leaking tons of memory if I use Typefaces together with androidsvg library, where I load multiple fonts using the SVGExternalFileResolver.
  • Adrian L
    Adrian L over 9 years
    That was it in my case! Thanks
  • user936580
    user936580 over 8 years
    With that it doesn't work because I have it in the font directory. I've tried to put it in the root directory but it fails anyway.
  • 0101100101
    0101100101 about 8 years
    Also fixes it for createFromFile