如何使用charset windows-1251将原始资源加载到WebView中?

时间:2012-11-05 14:33:09

标签: android webview

如何使用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编码?

1 个答案:

答案 0 :(得分:0)

如果您动态创建Web内容,则在

部分中设置以下字符集可能会有所帮助
String header = "<HTML><HEAD><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1251\"></HEAD>";