2
我使用WebView
来呈现某些html内容。在我的CSS,我有以下代码:Lollipop中的WebView不会从资产中加载字体
@font-face {
font-family: CharterC;
font-style: normal;
font-weight: normal;
src: url("asset://fonts/CharterC.otf");
}
而且我在WebViewClient
下面的Java代码:
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (url.startsWith(ASSETS_SCHEME)) {
String asset = url.substring(ASSETS_SCHEME.length());
if (asset.endsWith(".js")) {
try {
return new WebResourceResponse("text/javascript", "utf-8", context.getAssets().open(asset));
} catch (Exception e) {
e.printStackTrace();
}
} else if (asset.endsWith(".ttf") || asset.endsWith(".otf")) {
try {
return new WebResourceResponse("font/" + MimeTypeMap.getFileExtensionFromUrl(asset), "utf-8", context.getAssets().open(asset)); // this will produce MimeType like: font/ttf
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
而且此代码的工作相当不错,在Android的4.1-4.4,但在Android的5.0此代码加载JS,但无法加载字体,和我看到的WebKit控制台以下消息:
从原产字体的资产://“已经加载由 跨域资源受阻共享策略:没有“访问控制 - 允许来源” 标题出现在请求的资源上。原因'null'是 ,因此不允许访问。
如何从Android 5.0资产加载字体?