32

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?

hata
  • 11,633
  • 6
  • 46
  • 69
gurkan stack
  • 413
  • 1
  • 15
  • 43

3 Answers3

80

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

gurkan stack
  • 413
  • 1
  • 15
  • 43
  • 2
    The weird part is, loadData is working with debug build. It only cease to work on release build. – neobie Aug 09 '19 at 13:58
  • @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. – AlexanderShirshov Jul 11 '20 at 06:30
  • 1
    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 – Trake Vital Jul 22 '20 at 18:49
38

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.

Yuichi Araki
  • 3,438
  • 1
  • 19
  • 24
9

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

dna
  • 537
  • 3
  • 8