My favorites | Sign in
Project Home Issues
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 3552: WebView.loadData() doesn't show Japanese characters
10 people starred this issue and may be notified of changes. Back to list
Status:  Declined
Owner:  cary%and...@gtempaccount.com
Closed:  Aug 2009


Sign in to add a comment
 
Reported by ishik...@digital-genes.com, Aug 13, 2009
Android version: 1.5_r3
OS: MS Windows XP SP3
The following code doesn't display any Japanese characters.

WebView wv = new WebView(this);
wv.loadData("<html><body>漢字テスト</body></html>", "text/html", "utf-8");

While the following code displays Japanese characters properly.

TextView tv = new TextView(this);
tv.setText("漢字テスト");
setContentView(tv);
Aug 14, 2009
#1 jbq+legacy@google.com
(No comment was entered for this change.)
Labels: Component-Browser
Aug 17, 2009
#2 tetsu...@gmail.com
I think you need to escape the japanese text in UTF-8 for loadData().

  wv.loadData("<html><body>漢字テスト</body></html>", "text/html", "utf-8");

Aug 18, 2009
#3 cccandr...@gmail.com
agreed; characters as shown is not encoded as utf-8
Status: Declined
Owner: c...@android.com
Aug 18, 2009
#4 ishik...@digital-genes.com
Whatever code you see in the post is, the original code is in UTF-8.
Because, I tested it after I saved the source file in UTF-8.

Originally, I tried the following code but it didn't show the Japanese
characters. In this case, res/raw/help_jp.html is written in UTF-8.
html.loadUrl(sever_url + "/help_jp.html") shows the page correctly.

public class MyHtmlpage extends Activity {
    static final int MAX_HTML_SIZE = 4096;
    static final String ENCODING = "utf-8";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        WebView html = new WebView(this);
        InputStream is = getResources().openRawResource(R.raw.help_jp);
        byte[] buff = new byte[MAX_HTML_SIZE];
        int len;
        String rawText;
        try {
            len = is.read(buff, 0, MAX_HTML_SIZE);
            rawText = EncodingUtils.getString(buff, 0, len, ENCODING);
        } catch (Exception e) {
            rawText = "";
        }
        html.loadData(rawText, "text/html", "utf-8");
        setContentView(html);
    }

}

Aug 19, 2009
#5 cccandr...@gmail.com
Thanks for the additional diagnosis. Please note that the implementation for loadData is this:
    public void loadData(String data, String mimeType, String encoding) {
        loadUrl("data:" + mimeType + ";" + encoding + "," + data);
    }
This means that 'data' needs to be URL encoded, since it is being passed to the browser in the same way you 
would type a web address. You may have more luck using this interface:
    public void loadData(String data, String mimeType, String encoding) {
        loadUrl("data:" + mimeType + ";" + encoding + "," + data);
    }

Aug 20, 2009
#6 ishik...@digital-genes.com
Thanks for the suggestion. But it still has a problem. Blanks in the
URLencoded string appeared in the screen as "+" instead of a space.
Now the following code shows "漢字+テスト" instead of "漢字 テスト"
that I want to see on the screen. Any idea?

html.loadData(URLEncoder.encode("<html><body>漢字 テスト</body></html>", "utf-8"),
"text/html", "utf-8");
setContentView(html);


Aug 20, 2009
#7 ishik...@digital-genes.com
OK, I found a simple work around. The following code shows the text just fine.
Thanks for everyone.

webview.loadData(URLEncoder.encode("<html><body>漢字 テスト</body></html>",
"utf-8").replaceAll("\\+"," "), "text/html", "utf-8");

Mar 19, 2010
#9 blued...@gmail.com
korean has same problem. so i try to add meta tag to html string

<meta http-equiv="Content-Type" content="text/html; charset=utf-16le">

in this case, solution is

wv.loadData("<html><meta http-equiv=\"Content-Type\" content=\"text/html; 
charset=utf-16le\"><body>漢字テスト</body></html>", "text/html", "utf-8");

i think android may ignore encording params in loadData()


Aug 23, 2010
#10 cary%and...@gtempaccount.com
(No comment was entered for this change.)
Owner: c...@android.com
Dec 28, 2010
#11 eden...@gmail.com
Slightly ridiculous that the <meta> tag is required to set the encoding appropriately, especially when loadData has an encoding param.  If it's not specified in the html, the WebView should default to the param value.
May 2, 2011
#12 andrey.y...@gmail.com
Thanks to all! Meta tagging the html solved my issue with non ASCII chars as well.
Apr 26, 2012
#13 wuch...@gmail.com
It seems that solution wiil fail in android 4.0!!
Jul 12, 2012
#14 deni...@gmail.com
Reading the docs at http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String, java.lang.String, java.lang.String), loadData() accepts only base64 or ASCII encodings, but loadDataWithBaseURL() can accept other encodings.

So, the following is a bit hacky but works for me:

wv.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
Apr 15, 2013
#15 regis.de...@gmail.com
Alternative workaround for Android 4.0+

webView.loadData(htmlContent, "text/html; charset=UTF-8", "UTF-8")
Sign in to add a comment

Powered by Google Project Hosting