WebView.loadData not working on Android 9.0 (API-29)

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.


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


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