一、前提 OV手机低于android 7.0版本,
二、baseUrl是Https的
三、使用: webView.loadDataWithBaseURL(baseUrl, html, "text/htmlcharset=UTF-8", "utf-8", null) 方法加载
解决办法
还有一种不太好的解决办法: webView.loadDataWithBaseURL(null, html, "text/htmlcharset=UTF-8", "utf-8", null)
但是这样会导致页面内不是完整路径的图片找不到。
第一步:将资源拷贝到assets目录第二步:给需要注入的资源文件在url上做一个标志,当然,你也可以不做,只要你在安卓端可以判断出来即可。第三步:拦截将要注入的文件,读取本地文件即可。webview.setWebViewClient(newWebViewClient(){@OverridepublicWebResourceResponseshouldInterceptRequest(WebViewview,Stringurl){//System.out.println("WebResourceResponse::"+url)if(url.contains("[inject]")){StringlocalPath=url.replaceFirst("^http.*inject\\]","")System.out.println("LocalPath::"+localPath)try{InputStreamis=getApplicationContext().getAssets().open(localPath)returnnewWebResourceResponse("text/javascript","UTF-8",is)}catch(Exceptione){e.printStackTrace()returnsuper.shouldInterceptRequest(view,url)}}else{returnsuper.shouldInterceptRequest(view,url)}}})android中webview加载html用本地css渲染的做法:1:将资源拷贝到assets目录
2:给需要注入的资源文件在url上做一个标志,当然,你也可以不做,只要你在安卓端可以判断出来即可。
<link rel="stylesheet" href="[inject]public/css/bootstrap.min.css" type="text/css" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="[inject]public/css/bootstrap-theme.min.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script type="text/javascript" src="[inject]public/js/zipto/1.1.6/zepto.min.js"></script>
<script type="text/javascript" src="[inject]public/js/modules/md5.min.js"></script>
3:拦截将要注入的文件,读取本地文件即可。
webview.setWebViewClient(new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
//System.out.println("WebResourceResponse::"+url)
if(url.contains("[inject]")){
String localPath = url.replaceFirst("^http.*inject\\]","")
System.out.println("LocalPath::"+localPath)
try {
InputStream is = getApplicationContext().getAssets().open(localPath)
return new WebResourceResponse("text/javascript", "UTF-8", is)
} catch (Exception e) {
e.printStackTrace()
return super.shouldInterceptRequest(view, url)
}
} else {
return super.shouldInterceptRequest(view, url)
}
}
})