如何将 charset windows-1251 的原始资源加载到 WebView 中?对我有用的唯一方法是:
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编码?