gur*_*ack 6 java android webview android-webview android-9.0-pie
我有一个文章应用程序,我在webview中显示文章.在Android版本8.1和9.0中这个webview无法正常工作.该应用程序在文章活动中显示NOTHING.
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");
Run Code Online (Sandbox Code Playgroud)
结果,我该如何解决这个问题?
gur*_*ack 15
我解决了问题,该问题出现在具有最新版Chrome的智能手机中。
解:
不使用
mWebview.loadData
方法,改为使用
mWebview.loadDataWithBaseURL
结果,我的解决方案是:
mWebview.loadDataWithBaseURL(null,htmlContent,"text/html", "utf-8", null);
您的HTML内容应为Base64或URL编码.您的HTML示例中包含"#",这会导致某些WebView版本出现问题.
这是Base64编码的一个例子.
String htmlContent = "...";
String encodedHtml = Base64.encodeToString(htmlContent.getBytes(), Base64.NO_PADDING);
webView.loadData(encodedHtml, "text/html", "base64");
Run Code Online (Sandbox Code Playgroud)
这是javadoc的细节.
小智 5
我也有与Android版本9.0相同的问题
此页面上的文档(https://developer.android.com/about/versions/pie/android-9.0-migration)提到:
在Android 9中,用于Java语言的UTF-8解码器更加严格,并遵循Unicode标准。
所以我尝试将UTF-8转换为Base64并使用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();
}
Run Code Online (Sandbox Code Playgroud)
现在它照常工作。
希望能帮助到你
| 归档时间: |
|
| 查看次数: |
2292 次 |
| 最近记录: |