Android WebView garbled characters when loading local pages

Source: Internet
Author: User

Android itself comes with a WebView control, which is used to display webpage content in the application during development. The web content or local content can be displayed, however, when a local page is displayed, the WebView does not know the encoding used to render the page content because it does not have an http header. The solution is to specify the display encoding for WebView, and set the encoding for WebView in Settings, for example:

 

1 mWebView = (WebView) findViewById (R. id. webview );
2 mWebView. getSettings (). setJavaScriptEnabled (true );
3 mWebView. getSettings (). setdefatextextencodingname ("UTF-8 ");
4 mWebView. loadUrl (path );

 

 

 

The page will be displayed in UTF-8 encoding

 

Note that if the local page is loaded, the path must be specified as the file Protocol, for example, "file: // data/com. mytest/1.web"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.