android webview loadDataWithBaseURL 方法在4.1、4.3的误用

版权声明:转载请邮件联系我([email protected]),以取得授权,谢谢! https://blog.csdn.net/yeshennet/article/details/86620091

最近在弄一个全屏幕的webview,然后MTL的同事报了一个错误,说webview那里直接显示代码出来了。大概是这样子。
在这里插入图片描述
哦,先说下背景。

这里要加载本地的一些html文件,显示成弹窗的样子,所以背景是透明的。为了避免utf-8的问题,我是这样写的。

WebSettings settings = mWebView.getSettings();
settings.setDefaultTextEncodingName("utf-8");

mWebView.loadDataWithBaseURL("http://yeshen.org",
   getString(R.string.info_texto), 
   "text/html; charset=utf-8", "");

网上找不到这个方法的资料,发散思维想了许久。看了官方的API文档。

loadDataWithBaseURL

最后发现是,mimeType 那里写错了,应该是这样写

mWebView.loadDataWithBaseURL("http://yeshen.org",
   getString(R.string.info_texto), 
   "text/html", null);

OK,总结下,loadDataWithBaseURL 的 mimeType 写成 “text/html; charset=utf-8” 在Android4.1、android4.3 上会出现上图的效果,修正了就没事了。

猜你喜欢

转载自blog.csdn.net/yeshennet/article/details/86620091