简体   繁体   中英

How to load raw resource with charset windows-1251 into 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;
   }
}

and then

String html = FileUtils.loadRawFileAsBase64(this, R.raw.htmlfile);
WebView webView = (WebView) findViewById(R.id.webview);
webView.loadData(html, "text/html; charset=windows-1251", "base64");

Is there a way to avoid base64 encoding?

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

String header = "<HTML><HEAD><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1251\"></HEAD>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM