Android WebView with garbled UTF-8 characters.

AndroidWebviewUtf

Android Problem Overview


I'm using some webviews in my android app, but are unable to make them display in utf-8 encoding.

If use this one I won't see my scandinavian charcters:

mWebView.loadUrl("file:///android_asset/om.html")

And if try this one, I won't get anything displayed at all

mWebView.loadDataWithBaseURL("file:///android_asset/om.html", null, "text/html", "utf-8",null);

Regards

Android Solutions


Solution 1 - Android

You can try to edit the settings of your webview before you load the data:

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

Also, as provided in the comment below, be sure to add "charset=utf-8" to the loadData call:

mWebView.loadData(getString(R.string.info_texto), "text/html; charset=utf-8", "utf-8");

Solution 2 - Android

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

Use loadDataWithBaseURL instead of loadData.

// 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 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 include charset=utf-8.

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

You can also use this formulation with WebSettings

WebView webView = (WebView) findViewById(R.id.DemoWebView);
WebSettings webSettings = webView.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");  
webView.loadData(scandinavianCharacters, "text/html; charset=utf-8", null);

It is amazing that Android still hasn't resolved this basic issue.

Solution 3 - Android

Derzu's bit is very helpful above:

webview.loadData(getString(R.string.info_texto), "text/html; charset=utf-8", "utf-8");

I had UTF-8 on Android 2.x and garbled ANSI on 4.x until I put in the

 charset=utf-8

in the wv.loadUrlWhatever() call. Excellent attention to detail, Derzu

Solution 4 - Android

There are two ways that a HTML page delivered by a HTTP server can specify the content encoding. Usually, the server will specify the content encoding in the HTTP headers, but since this page is being loaded from a file, there is no HTTP transaction and therefore no headers. As a result, WebView assumes a default encoding of Latin-1.

However, you can specify a content encoding using the <meta> tag. Construct your html file thus:

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Title</title>
</head>
Your content following

And then load it into WebView using mWebView.loadUrl("file:///android_asset/om.html");. It should display the non-latin characters as you expect.

Solution 5 - Android

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

A combination of the two seems to work for me. For some reason it likes null on the encoding and the charset in the mime type :/ weird. this has solved months of aggravation for me.

Solution 6 - Android

You need to swap your first two arguments. See this thread: https://stackoverflow.com/questions/3312643/android-webview-utf-8-not-showing

So your code should look like this:

mWebView.loadDataWithBaseURL(null, "file:///android_asset/om.html", "text/html", "utf-8",null);

Solution 7 - Android

You should keep 3 things in mind to show the right content always:

  1. Using loadDataWithBaseUrl instead of loadData funciton.
  2. Setting the correct encoding in html file as a meta tag
  3. Setting defaultTextEncodingName in WebSettings

The examples have been provided via other answers so I don't repeat!

Solution 8 - Android

I'm not sure what you are doing prior to loading that page. Could this security change have anything to do with it? Are you loading page from web before?

Note for post 1.0. Due to the change in the WebKit, the access to asset files through "file:///android_asset/" for the sub resources is more restricted. If you provide null or empty string as baseUrl, you won't be able to access asset files. If the baseUrl is anything other than http(s)/ftp(s)/about/javascript as scheme, you can access asset files for sub resources.

Taken from here: http://developer.android.com/reference/android/webkit/WebView.html In the section on method "loadDataWithBaseURL".

Can you use "loadData" instead for a quick test? Specify "utf-8" for the encoding and pasting a scandinavian character into the data parmeter. Simple test to remove the security issue.

Solution 9 - Android

mwebView.loadData(URLEncoder.encode(data, "utf-8").replaceAll("\\+"," "), "text/html", "utf-8");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionelwisView Question on Stackoverflow
Solution 1 - AndroidEric NordvikView Answer on Stackoverflow
Solution 2 - AndroidCameron Lowell PalmerView Answer on Stackoverflow
Solution 3 - AndroidR Earle HarrisView Answer on Stackoverflow
Solution 4 - AndroidAaron DView Answer on Stackoverflow
Solution 5 - Androiduser2860495View Answer on Stackoverflow
Solution 6 - AndroidSparkyView Answer on Stackoverflow
Solution 7 - AndroidAli HashemiView Answer on Stackoverflow
Solution 8 - AndroidShakakaiView Answer on Stackoverflow
Solution 9 - AndroidPPDView Answer on Stackoverflow