Android WebView不加载图片资源的解决办法

来源:互联网 发布:postgresql 源码安装 编辑:程序博客网 时间:2024/05/29 13:40

最近项目中需要控制列表页和详情页图片资源的显示,列表页比较好做,详情页是用WebView来展示的,不太好控制图片资源的加载。在Google上找到了两个解决办法,跟大家分享一下!

1、WebSettings.setBlockNetworkImage(boolean flag)

//不加载网络上的图片资源mWebView.getSettings().setBlockNetworkImage(true);

将flag设置为true即可达到目的

官方API解释如下:

Sets whether the WebView should not load image resources from the network (resources accessed via http and https URI schemes). Note that this method has no effect unless getLoadsImagesAutomatically returns true. Also note that disabling all network loads using setBlockNetworkLoads will also prevent network images from loading, even if this flag is set to false. When the value of this setting is changed from true to false, network images resources referenced by content currently displayed by the WebView are fetched automatically. The default is false.


2、mWebView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, failUrl)

先通过http请求获取要显示HTML页面的所有文本内容,然后通过正则去过滤img标签,替换src属性为默认的图片链接,替换完成后使用mWebView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, failUrl)来显示。


总的来说,第二种方法稍微麻烦一点,但是更灵活一些,可根据项目需要来决定用哪种方法。




0 0
原创粉丝点击