1

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

4

1 回答 1

0

如果您动态创建 Web 内容,则在部分中设置以下字符集可能会有所帮助

String header = "<HTML><HEAD><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1251\"></HEAD>";
于 2013-11-09T20:07:04.977 回答