本文介绍了如何1251窗口加载原始资源与字符集到的WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何1251窗口加载原始资源与字符集到的WebView?这是为我工作的唯一方法是:
How to load raw resource with charset windows-1251 into WebView? The only way that is working for me is:
public class FileUtils {
public static String loadRawFileAsBase64(Context context, int id) throws IOException {
return Base64.encodeToString(loadRawFileAsByteArray(context, id), Base64.DEFAULT);
}
public static byte[] loadRawFileAsByteArray(Context context, int id) throws IOException {
byte[] result = null;
Resources resources = context.getResources();
InputStream inputStream = resources.openRawResource(id);
ByteArrayOutputStream content = new ByteArrayOutputStream();
try {
byte[] sBuffer = new byte[512];
int readBytes = 0;
while ((readBytes = inputStream.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes);
}
result = content.toByteArray();
} finally {
content.close();
}
return result;
}
}
然后
String html = FileUtils.loadRawFileAsBase64(this, R.raw.htmlfile);
WebView webView = (WebView) findViewById(R.id.webview);
webView.loadData(html, "text/html; charset=windows-1251", "base64");
有没有办法来避免base64编码?
Is there a way to avoid base64 encoding?
推荐答案
如果您创建网页内容动态可能会有所帮助,设置以下章节字符集
If you create web content dynamically it may be helpful to set following charset in section
String header = "<HTML><HEAD><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1251\"></HEAD>";
这篇关于如何1251窗口加载原始资源与字符集到的WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!