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

最近项目中需要控制列表页和详情页图片资源的显示,列表页比较好做,详情页是用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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值