我有这段代码可以从assets文件夹中获取字体文件:
public static Typeface getMyFont(Context context, String resource) {
InputStream is;
Typeface font = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
AssetManager assetManager = context.getResources().getAssets();
try {
is = classLoader.getResourceAsStream(resource);
is = assetManager.open(resource);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
Log.e("wwwww", line);
}
br.close();
font = Typeface.createFromAsset(context.getAssets(), line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return font;
}
这里的资源是链接(快捷方式)的assets文件夹中的font / MYFONT.ttf,但我在这一行得到空指针异常:
font = Typeface.createFromAsset(context.getAssets(), line);