34

I have an article app, I am showing articles in WebView. In Android version 9.0 (API-29) this WebView is not working. The app shows NOTHING in my article Activity.

mWebView.setVisibility(View.VISIBLE); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setLoadWithOverviewMode(true); mWebView.getSettings().setUseWideViewPort(true); mWebView.getSettings().setMinimumFontSize(14); String htmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + "<head>" + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />" + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>" + "<style type=\"text/css\">body{color: #525252;} img {max-width: 100%; height: auto}</style>" + "</head>" + item.getContent() //content of item + ""; mWebView.loadData(htmlContent, "text/html; charset=utf-8", "UTF-8"); 

As a result, how can I solve this problem?

3 Answers 3

85

I solved my problem, the problem occurs in Smartphones that has latest Chrome.

SOLUTION:

Do not use

mWebview.loadData

method, instead use

mWebview.loadDataWithBaseURL

As a result my solution is:

mWebview.loadDataWithBaseURL(null,htmlContent,"text/html", "utf-8", null);

Sign up to request clarification or add additional context in comments.

3 Comments

The weird part is, loadData is working with debug build. It only cease to work on release build.
@neobie any chance you resolved issue with release build? In my case both loadData and loadDataWithBaseUrl not working with any parameters in release build, only in debug build. And it's also specific for project, cause test project created from scratch show local content with webView in release build perfectly fine.
I included the baseUrl (the url where I originally got the html from) instead of keeping it null. My guess is that the problem was, that whenever the webview tried to get images for the html then he wouldn't know where to look but after I gave it the original url then it knew where to find them
39

Your HTML content should be either Base64 or URL encoded. Your HTML example has a "#" in it, and it causes the problem on some WebView versions.

Here's an example with Base64 encoding.

String htmlContent = "..."; String encodedHtml = Base64.encodeToString(htmlContent.getBytes(), Base64.NO_PADDING); webView.loadData(encodedHtml, "text/html", "base64"); 

Here's javadoc for detail.

3 Comments

Saved my life bro!
this should be the accepted answer. Thank you sir.
Great! This should be the accepted answer, my web view stoped to work when I upgraded to 29 from 28, you save my day! Thanks a lot
10

I too had the same problem with Android Version 9.0

The documents at this page (https://developer.android.com/about/versions/pie/android-9.0-migration) mention that:

In Android 9, the UTF-8 decoder for Java language is stricter and follows the Unicode standard.

So I tried converting the UTF-8 into Base64 and use loadData()

try { String base64 = null; base64 = android.util.Base64.encodeToString(lecureHtmlData.getBytes("UTF-8"), android.util.Base64.DEFAULT); wvLecture.loadData(base64, "text/html; charset=utf-8", "base64"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } 

Now it is working as usual.

Hope it helps

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.