一行代码让webview不加载图片

2019-01-26 10:12: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)来显示。




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

--------------------- 

作者:Ricky_Fung 

来源:CSDN 

原文:https://blog.csdn.net/top_code/article/details/20467091 

版权声明:本文为博主原创文章,转载请附上博文链接!

  • 2018-03-07 14:35:32

    centos7下yum安装ffmpeg

    安装EPEL Release,因为安装需要使用其他的repo源,所以需要EPEL支持 yum install -y epel-release

  • 2018-03-08 09:44:12

    前端性能监控:window.performance

    Web Performance API允许网页访问某些函数来测量网页和Web应用程序的性能,包括 Navigation Timing API和高分辨率时间数据。

  • 2018-03-08 09:44:15

    前端性能监控:window.performance

    Web Performance API允许网页访问某些函数来测量网页和Web应用程序的性能,包括 Navigation Timing API和高分辨率时间数据。

  • 2018-03-08 09:47:14

    ES6,Array.fill()函数的用法

    ES6为Array增加了fill()函数,使用制定的元素填充数组,其实就是用默认内容初始化数组。

  • 2018-03-08 09:53:39

    document.readyState

    一个document 的 Document.readyState 属性描述了文档的加载状态。

  • 2018-03-09 02:09:23

    ArrayBuffer:类型化数组

    ArrayBuffer对象、TypedArray对象、DataView对象是JavaScript操作二进制数据的一个接口。这些对象早就存在,属于独立的规格,ES6将它们纳入了ECMAScript规格,并且增加了新的方法。

  • 2018-03-09 11:45:11

    SQL SELECT DISTINCT 语句

    如需从 Company" 列中仅选取唯一不同的值,我们需要使用 SELECT DISTINCT 语句: