一行代码让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-01-18 08:54:06

    利用localStorage来处理你的Javascript脚本错误

    localStorage作为HTML5中的新特性,它的出现可以说对于前端性能体验来讲可以获得相当大的改善。存储一些用户经常用到而又不是涉及隐私和安全的东西,的确是一个非常好的选择。下面分享一下用localStorage来处理客户端代码错误的例子来了解一下它的用途吧。

  • 2020-01-20 08:29:14

    js如何生成唯一标识符UUID

    在JavaScript中生成uuid的代码如下,这个函数会直接给你返回uuid,所以直接调用,然后用变量接收即可!

  • 2020-01-30 11:19:58

    Android中添加两个(多个)FileProvider节点问题

    我们知道在android7.0,修改了对私有存储的限制,导致在获取资源的时候,不能通过Uri.fromFile(..)来获取uri了,但是在写入数据的时候是可以通过Uri.fromFile(..)来获取uri的,android 官网给出的解决办法是通过FileProvider来解决这一问题,我们需要在AndroidManifest.xml 配制provider节点。