我正在努力了解Volley的图像缓存.我有一个包含gridview的片段,它将加载大约12-30个图像.从服务器检索图像,我正在使用Network
ImageView加载这些图像.
我可以在NetworkImageView中显示图像,一切正常.但是,当我从一个片段更改为另一个片段并返回到上一个片段时,在LogCat中,我看到Volley正在尝试再次获取图像.
我读了Volley自动处理Image Caching.当图像缓存在第一个片段中时,为什么要再次获得图像,当我从第二个到第一个回来时呢?首先是LogCat数据,显示Volley的图像请求?或者是其他东西…
以下是我的代码:
在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); } });@H_403_11@Logcat在第一次加载片段时:
02-18 14:21:20.724: D/Volley(14713): [4944] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://xx.files.wordpress.com/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=<[ ] http://xx.files.wordpress.com/2014/02/st-vincent.jpg 0x800c5bdc LOW 3> [lifetime=3941],[size=501475],[retryCount=0] 02-18 14:21:20.894: D/Volley(14713): [1] Request.finish: 4181 ms: [ ] http://xx.files.wordpress.com/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: [ ] http://xx.files.wordpress.com/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: [ ] http://xx.files.wordpress.com/2014/02/underwater.gif 0x800c5bdc LOW 8 02-18 14:21:21.214: D/Volley(14713): [4945] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://xx.files.wordpress.com/2014/02/screen-shot-2014-02-17-at-4-24-04-pm.png 0x800c5bdc LOW 5> [lifetime=4155],[size=482380],[retryCount=0] 02-18 14:21:21.244: D/Volley(14713): [1] Request.finish: 4494 ms: [ ] http://xx.files.wordpress.com/2014/01/albarn-everyday-robots.jpg 0x800c5bdc LOW 9 02-18 14:21:21.274: D/Volley(14713): [1] Request.finish: 4551 ms: [ ] http://xx.files.wordpress.com/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: [ ] http://xx.files.wordpress.com/2014/02/macdemarco_baby.jpg 0x800c5bdc LOW 10 02-18 14:21:22.934: D/Volley(14713): [1] Request.finish: 6183 ms: [ ] http://xx.files.wordpress.com/2014/01/nenehcherry_lank01.jpg 0x800c5bdc LOW 11@H_403_11@当我第二次回到同一个片段时:刚刚访问第二个片段并回到第一个片段 – 之间没有太大的差距.
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: [ ] http://xx.files.wordpress.com/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=<[ ] http://xx.files.wordpress.com/2014/02/screen-shot-2014-02-17-at-11-57-29-pm.png 0x800c5bdc LOW 2> [lifetime=3272],[retryCount=0] 02-18 14:27:48.484: D/Volley(14713): [1] Request.finish: 3456 ms: [ ] http://xx.files.wordpress.com/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: [ ] http://xx.files.wordpress.com/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: [ ] http://xx.files.wordpress.com/2014/01/albarn-everyday-robots.jpg 0x800c5bdc LOW 9 02-18 14:27:49.374: D/Volley(14713): [1] Request.finish: 4325 ms: [ ] http://xx.files.wordpress.com/2014/01/nenehcherry_lank01.jpg 0x800c5bdc LOW 11 02-18 14:27:49.404: D/Volley(14713): [1] Request.finish: 4355 ms: [ ] http://xx.files.wordpress.com/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: [ ] http://xx.files.wordpress.com/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: [ ] http://xx.files.wordpress.com/2014/02/echo-and-the-bunnymen.jpg 0x800c5bdc LOW 12@H_403_11@正如链接所示,Volley正在访问相同的URL. Volley是否尝试从服务器获取图像?还是只是显示从缓存加载的url?
如何制作Volley缓存图像?如果现在不处理缓存,使用我的代码,我该怎么做才能实现?
我已经尝试将maxSize值从10改为100 * 1024 * 1024(100MB),但是没有停止从Volley输出相同的值.
解决方法
Volley没有直接提供缓存选项.你必须在Volley提供的工具中做出自己的一切.见
Network Image caching,杰克·沃顿(Jake Wharton)曾经写过关于使用Volley的缓存机制.
Jake Wharton’s Volley Customization