Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...