Android WebView with malformed UTF-8 characters.

I use some webviews in my Android application, but cannot make them appear in utf-8 encoding.

If you use this, I will not see my Scandinavian characters:

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

And if you try this, I won’t see anything at all

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

Hello

+66
android webview utf
Feb 08 2018-11-12T00:
source share
9 answers

You can try changing your web browser settings before downloading data:

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

Also, as noted 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"); 
+127
Feb 08 2018-11-11T00:
source share
β€” -

It 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 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"); 

You can also use this wording 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); 

Surprisingly, Android still has not resolved this underlying issue.

+117
May 31 '12 at 10:05 a.m.
source share

Derzu bit is very useful above:

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

I had utf-8 on Adroid 2.x and distorted ansi on 4.x until I turned on

  charset=utf-8 

in a call to wv.loadUrlWhatever (). Great attention to detail, Derzu

+25
Apr 16 '13 at 19:04 on
source share

There are two ways in which the HTML page provided by the HTTP server can indicate the encoding of the content. Typically, the server will indicate the encoding of the content in the HTTP headers, but since this page is loaded from a file, there are no HTTP transactions, and therefore no headers. As a result, WebView assumes default Latin-1 encoding by default.

However, you can specify the encoding of the content using the <meta> . Build your html file this way:

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

Then upload it to WebView using mWebView.loadUrl("file:///android_asset/om.html"); . It should display non-latin characters as you expect.

+11
Apr 28 '13 at 7:50
source share
 WebView wv = (WebView) findViewById(R.id.rowWebview); WebSettings settings = wv.getSettings(); settings.setDefaultTextEncodingName("utf-8"); wv.loadData(topHtml, "text/html; charset=utf-8",null); 

The combination of the two seems to work for me. For some reason, he likes null in encoding and encoding in type mime: / strange. this allowed me to aggravate the months of exacerbation.

+9
08 Oct '13 at
source share

You need to change your first two arguments. See this topic: 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); 
+2
Feb 24 2018-12-12T00: 00Z
source share

I'm not sure what you are doing before loading this page. Could this security change have anything to do with it? Do you download a page from the Internet before?

Note for post 1.0. Due to a change in WebKit, access to asset files through "file: /// android_asset /" for auxiliary resources is more limited. If you provide an empty or empty string as baseUrl, you will not be able to access the asset files. If baseUrl is something other than http (s) / ftp (s) / about / javascript as a schema, you can access resource files for auxiliary resources.

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

Can you use "loadData" instead of a quick test? Specify "utf-8" to encode and insert the Scandinavian character in the data parameter. A simple test to fix a security problem.

0
Feb 08 '11 at 13:13
source share

There are three things to keep in mind to always display the right content:

  • Using loadDataWithBaseUrl instead of the loadData function.
  • Setting the correct encoding in the html file as a meta tag
  • Setting defaultTextEncodingName in WebSettings

Examples were provided with other answers, so I am not repeating!

0
Jul 6 '14 at 14:20
source share
 mwebView.loadData(URLEncoder.encode(data, "utf-8").replaceAll("\\+"," "), "text/html", "utf-8"); 
0
Apr 24 '15 at 9:19
source share



All Articles