Android WebView UTF-8 not showing

I have a webview and am trying to load plain UTF-8 text into it.

mWebView.loadData("將賦予他們的傳教工作標示為", "text/html", "UTF-8"); 

But WebView displays ANSI / ASCII garbage.

Obviously, the problem is with the encoding, but what I am missing is that you specify a webview to display text in Unicode?

This is a HelloWorld application.

+36
android unicode webview
Jul 22. '10 at 19:31
source share
2 answers

Using:

 mWebView.loadDataWithBaseURL(null, "將賦予他們的傳教工作標示為", "text/html", "utf-8", null); 

or using WebSettings with setDefaultTextEncoding :

 WebSettings settings = mWebView.getSettings(); settings.setDefaultTextEncodingName("utf-8"); 

For the latest versions of Android, the API from 16 to 22, it was tested and works correctly using the loadData () method, so mimeType should include: "charset = utf-8".

 WebView mWebView = (WebView) findViewById(R.id.myWebView); WebSettings settings = mWebView.getSettings(); settings.setDefaultTextEncodingName("utf-8"); mWebView.loadData(myCharacters, "text/html; charset=utf-8",null); 

or

  mWebView.loadData(myCharacters, "text/html; charset=utf-8","UTF-8"); 
+111
Jul 22 2018-10-22T00:
source share

This problem dates back to at least Gingerbread

It seems to have been broken in some form or fashion forever. Issue 1733

Use loadDataWithBaseURL instead of loadData h2>

 // Pretend this is an html document with those three characters String scandinavianCharacters = "øæå"; // Won't render correctly webView.loadData(scandinavianCharacters, "text/html", "UTF-8"); // Will render correctly webView.loadDataWithBaseURL(null, scandinavianCharacters, "text/html", "UTF-8", null); 

Now the part that is really annoying is that on Samsung Galaxy S II (4.0.3) loadData () works just fine, but testing on Galaxy Nexus (4.0.2) distorts multibyte characters if you don't use loadDataWithBaseURL (). WebView Documentation

Latest Android Versions

Some report a change in the behavior of loadData calls requiring mimeType to include charset=utf-8 .

 webView.loadData(scandinavianCharacters, "text/html; charset=utf-8", "UTF-8"); 

Discussion

The first time I saw this, my boss brought me his phone, an early Nexus, while I was developing at that time on the Samsung Galaxy II, and he appeared in our electronic news feed on his phone, which had a lot of - -ASCII. Thus, it is not only a long time in Android, but also not compatible between device manufacturers. This is the question when you need to program protection.

+5
Mar 06 '16 at 7:17
source share



All Articles