一行代码让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 

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

  • 2020-02-19 23:15:24

    PHP 闭包(Closure)

    闭包(Closure)又叫做匿名函数,也就是没有定义名字的函数。比如下面的例子:

  • 2020-02-19 23:26:58

    php array_pop 删除数组最后一个元素实例

    php array_pop函数将数组最后一个单元弹出(出栈),即删除数组的最后一个元素。本文章通过php实例向大家讲解array_pop函数的使用方法。

  • 2020-02-20 18:35:21

    Vue加载组件、动态加载组件的几种方式

    组件是Vue.js最强大的功能之一。组件可以扩展HTML元素,封装可重用的代码。在较高层面上,组件是自定义的元素,Vue.js的编译器为它添加特殊功能。在有些情况下,组件也可以是原生HTML元素的形式,以is特性扩展。