Home >Web Front-end >HTML Tutorial >Webview加载本地js、图片的方法_html/css_WEB-ITnose

Webview加载本地js、图片的方法_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:29:251328browse

在项目开发中经常会将比较大的js、图片、css等放到app中,而html放服务器,这样在使用时流量较少,加载也比都放服务器上快,其实方法也比较多,网上搜了很久都没结果。

一种是获取服务器返回的html内容后,直接处理返回的html,在引用js、图片的地方替换其资源为本地的“file:///android_asset/”资源,这种方式我没试。

另一种就是直接使用WebViewClient实现

 1 webView.setWebViewClient(new WebViewClient(){ 2             @Override 3             public WebResourceResponse shouldInterceptRequest(WebView view, String url) { 4                 WebResourceResponse response = null; 5  6                     response = super.shouldInterceptRequest(view,url); 7                     if (url.contains("m.css")){ 8                         try { 9                             response = new WebResourceResponse("text/css","UTF-8",getAssets().open("css/m.css"));10                         } catch (IOException e) {11                             e.printStackTrace();12                         }13                     }14                 return  response;15             }16 });

注意最好是使用if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)来区分下版本执行的代码

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn