Android 2.3 webview loadData only shows coded characters

I am trying to use webview with loadData to display an image:

String data = "<html><head><title>Photo</title></head>"; data = data + "<body><center><img width=\"100%\" src=\"" + imageUrl + "\" /></center></body></html>"; imageWebView.getSettings().setLoadWithOverviewMode(true); imageWebView.getSettings().setUseWideViewPort(true); imageWebView.getSettings().setBuiltInZoomControls(true); imageWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); imageWebView.setScrollbarFadingEnabled(false); imageWebView.loadData(data, "text/html", "UTF-8"); imageWebView.setBackgroundColor(0x00000000); 

In emulator 4.1, this works fine, and I see the image. In 2.3, it just displays the encoded html code.

+4
source share
1 answer

This is apparently caused by a known bug in WebView , where if you have any percentages in the data provided, the data is interpreted as a URL.

A known workaround, as mentioned in the bug report, replace all % with &#37; .

Another workaround that seems to work quite well is suggested in a similar SO post and should also cover any other characters that might cause the same problem:

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

Source: https://habr.com/ru/post/1438543/


All Articles