[英]How to get file from resource?
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.我在资源中保存了我的字体文件 *.ttf,我想在我的应用程序启动时取回它,但我不能这样做。 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?如何从资源中获取普通文件?
Have you placed in it assets folder?你把它放在assets文件夹中了吗? If you are looking forward to use it in TextView etc,:
如果您希望在 TextView 等中使用它,请执行以下操作:
Typeface typeface = Utils.getTypeface(context, textStyle);
if (typeface != null) setTypeface(typeface);
and the function in Utils file is: Utils文件中的函数是:
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:我的 ttf 文件的位置:
app\src\main\assets\fonts\font-file-name.ttf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.