I have a webview and am trying to load simple UTF-8 text into it.
mWebView.loadData("將賦予他們的傳教工作標示為", "text/html", "UTF-8");
But the WebView displays ANSI/ASCII garbage.
Obviously an encoding issue, but what am I missing in telling the webview to display the Unicode text?
This is a HelloWorld app.
Use:
or using WebSettings with setDefaultTextEncoding:
For recent versions of Android, API 16 to 22 it was tested and work properly using loadData() method, requires the mimeType to include: "charset=utf-8".
or
This problem goes back to at least Gingerbread
This seems to have been broken in some form or fashion forever. Issue 1733
Use loadDataWithBaseURL instead of loadData
Now the part that is truly annoying is that on the Samsung Galaxy S II (4.0.3) loadData() works just fine, but testing on the Galaxy Nexus (4.0.2) the multi-byte characters are garbled unless you use loadDataWithBaseURL(). WebView Documentation
Recent versions of Android
Some are reporting a change in the behavior of the loadData calls requiring the
mimeType
to includecharset=utf-8
.Discussion
The first time I saw this my boss brought me his phone, an early Nexus, while I was developing at the time on a Samsung Galaxy II and it showed up in our economic news feed on his phone which had a lot of non-ASCII characters. So, not only is this a long standing issue within Android, but it also isn't consistent between device makers. This is a matter where you have to program defensively.