齐射图像缓存

编程入门 行业动态 更新时间:2024-10-27 07:29:49
本文介绍了齐射图像缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试了解 Volley 的图像缓存.我有一个带有 gridview 的片段,它将加载大约 12-30 张图像.从服务器检索图像,我使用 NetworkImageView 加载这些图像.

I am trying to understand Volley's image caching. I have a fragment with gridview inside it, which will load around 12-30 images. There images are retrieved from server and i am using NetworkImageView for loading these images.

我能够在 NetworkImageView 中显示图像并且一切正常.但是,当我从一个 fragment 更改为另一个并返回到前一个片段时,在 LogCat 中,我看到 Volley 正在尝试再次获取图像.

I am able to display the images in the NetworkImageView and everything works fine. But, when I change from one fragment to another and come back to the previous fragment, in LogCat, I see that Volley is trying to fetch the images again.

我读过 Volley 会自动处理图像缓存.当图像缓存在第一个 fragment 中时,为什么它试图再次获取图像,当我从第二个返回到第一个时?首先,LogCat 数据是不是显示了 Volley 的图片请求?或者别的什么...

I read Volley automatically takes care of Image Caching. When the image is cached in the first fragment, why is it trying to get the image again, when i came back from second to first? In the first place, is the LogCat data, showing Volley's image requests? or something else...

下面是我的代码:

在onCreate()

queue = Volley.newRequestQueue(getActivity()); imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() { private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>( 10); public void putBitmap(String url, Bitmap bitmap) { mCache.put(url, bitmap); } public Bitmap getBitmap(String url) { return mCache.get(url); } });

第一次加载片段时的Logcat:

Logcat when loaded the fragment for first time :

02-18 14:21:20.724: D/Volley(14713): [4944] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] xx.files.wordpress/2014/02/screen-shot-2014-02-17-at-11-57-29-pm.png 0x800c5bdc LOW 2> [lifetime=3782], [size=398563], [rc=200], [retryCount=0] 02-18 14:21:20.874: D/Volley(14713): [4943] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] xx.files.wordpress/2014/02/st-vincent.jpg 0x800c5bdc LOW 3> [lifetime=3941], [size=501475], [rc=200], [retryCount=0] 02-18 14:21:20.894: D/Volley(14713): [1] Request.finish: 4181 ms: [ ] xx.files.wordpress/2014/02/screen-shot-2014-02-17-at-11-57-29-pm.png 0x800c5bdc LOW 2 02-18 14:21:20.974: D/Volley(14713): [1] Request.finish: 4260 ms: [ ] xx.files.wordpress/2014/02/st-vincent.jpg 0x800c5bdc LOW 3 02-18 14:21:20.994: D/dalvikvm(14713): GC_FOR_ALLOC freed 1914K, 6% free 68371K/72184K, paused 11ms, total 11ms 02-18 14:21:20.994: I/dalvikvm-heap(14713): Grow heap (frag case) to 72.368MB for 5843106-byte allocation 02-18 14:21:21.014: D/dalvikvm(14713): GC_FOR_ALLOC freed 1K, 5% free 74076K/77892K, paused 15ms, total 15ms 02-18 14:21:21.074: D/Volley(14713): [1] Request.finish: 4336 ms: [ ] xx.files.wordpress/2014/02/underwater.gif 0x800c5bdc LOW 8 02-18 14:21:21.214: D/Volley(14713): [4945] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] xx.files.wordpress/2014/02/screen-shot-2014-02-17-at-4-24-04-pm.png 0x800c5bdc LOW 5> [lifetime=4155], [size=482380], [rc=200], [retryCount=0] 02-18 14:21:21.244: D/Volley(14713): [1] Request.finish: 4494 ms: [ ] xx.files.wordpress/2014/01/albarn-everyday-robots.jpg 0x800c5bdc LOW 9 02-18 14:21:21.274: D/Volley(14713): [1] Request.finish: 4551 ms: [ ] xx.files.wordpress/2014/02/screen-shot-2014-02-17-at-4-24-04-pm.png 0x800c5bdc LOW 5 02-18 14:21:21.994: D/Volley(14713): [1] Request.finish: 5244 ms: [ ] xx.files.wordpress/2014/02/macdemarco_baby.jpg 0x800c5bdc LOW 10 02-18 14:21:22.934: D/Volley(14713): [1] Request.finish: 6183 ms: [ ] xx.files.wordpress/2014/01/nenehcherry_lank01.jpg 0x800c5bdc LOW 11

当我第二次回到同一个片段时:刚刚访问了第二个片段并回到了第一个片段 - 两者之间没有太大的差距..

When I come back to the same fragment the second time: Just visited second fragment and came back to first fragment - Not much a gap in between..

02-18 14:27:46.164: D/dalvikvm(14713): GC_FOR_ALLOC freed 29047K, 26% free 91776K/122752K, paused 23ms, total 23ms 02-18 14:27:47.994: D/dalvikvm(14713): GC_FOR_ALLOC freed 2957K, 21% free 97010K/122752K, paused 20ms, total 20ms 02-18 14:27:48.274: D/Volley(14713): [1] Request.finish: 3244 ms: [ ] xx.files.wordpress/2014/02/screen-shot-2014-02-17-at-2-58-16-pm.png 0x800c5bdc LOW 6 02-18 14:27:48.294: D/dalvikvm(14713): GC_FOR_ALLOC freed 2007K, 21% free 97932K/122752K, paused 14ms, total 14ms 02-18 14:27:48.324: D/Volley(14713): [4956] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] xx.files.wordpress/2014/02/screen-shot-2014-02-17-at-11-57-29-pm.png 0x800c5bdc LOW 2> [lifetime=3272], [size=398563], [rc=200], [retryCount=0] 02-18 14:27:48.484: D/Volley(14713): [1] Request.finish: 3456 ms: [ ] xx.files.wordpress/2014/02/screen-shot-2014-02-17-at-11-57-29-pm.png 0x800c5bdc LOW 2 02-18 14:27:48.974: D/dalvikvm(14713): GC_FOR_ALLOC freed 1030K, 15% free 104815K/122752K, paused 56ms, total 56ms 02-18 14:27:49.054: D/Volley(14713): [1] Request.finish: 4022 ms: [ ] xx.files.wordpress/2014/02/screen-shot-2014-02-17-at-4-24-04-pm.png 0x800c5bdc LOW 5 02-18 14:27:49.314: D/Volley(14713): [1] Request.finish: 4276 ms: [ ] xx.files.wordpress/2014/01/albarn-everyday-robots.jpg 0x800c5bdc LOW 9 02-18 14:27:49.374: D/Volley(14713): [1] Request.finish: 4325 ms: [ ] xx.files.wordpress/2014/01/nenehcherry_lank01.jpg 0x800c5bdc LOW 11 02-18 14:27:49.404: D/Volley(14713): [1] Request.finish: 4355 ms: [ ] xx.files.wordpress/2014/02/macdemarco_baby.jpg 0x800c5bdc LOW 10 02-18 14:27:49.654: D/dalvikvm(14713): GC_FOR_ALLOC freed 1456K, 12% free 108705K/122752K, paused 27ms, total 27ms 02-18 14:27:49.734: D/Volley(14713): [1] Request.finish: 4691 ms: [ ] xx.files.wordpress/2014/02/underwater.gif 0x800c5bdc LOW 8 02-18 14:27:50.304: D/dalvikvm(14713): GC_FOR_ALLOC freed 11584K, 16% free 103314K/122752K, paused 47ms, total 47ms 02-18 14:27:50.334: D/Volley(14713): [1] Request.finish: 5281 ms: [ ] xx.files.wordpress/2014/02/echo-and-the-bunnymen.jpg 0x800c5bdc LOW 12

如链接所示,Volley 正在访问相同的 URL.Volley 是否试图从服务器获取图像?或者它只是显示它从缓存加载的网址?

As the links show, Volley is accessing the same URLs. Is Volley trying to get images from server? or is it just showing the urls that it is loading from cache?

如何制作 Volley 缓存图像?如果它现在不处理缓存,使用上面的代码,我应该怎么做才能实现它?

How do I make Volley cache images? If it's not handling the caching right now, with my code above, what should i do to achieve it?

我尝试将 maxSize 值从 10 更改为 100*1024*1024 (100MB) 但这并没有阻止 Volley 输出相同的值.

I have tried changing the maxSize value from 10 to 100*1024*1024 (100MB) but that didn't stop from Volley outputting the same values.

推荐答案

Volley 没有直接给出缓存选项.您必须在 Volley 提供的工具中自行制作.请参阅 网络图像缓存,Jake Wharton 撰写了有关使用 Volley 的缓存机制的文章.杰克沃顿的截击定制

Volley did not gave caching option directly. you have to make your own with in the tool provide by Volley. See Network Image caching, Jake Wharton had written about caching mechanism using Volley. Jake Wharton's Volley Customization

更多推荐

齐射图像缓存

本文发布于:2023-11-16 14:20:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!