0

I am downloading images from server into the ListView and storing it into SD Card.

And when next time listview appears i am accessing it from SD card only using Async method, i use this approch so that every thing user does not need to access server.

But when all the images are being loaded into listview from SD Card and if i scroll it pretty fast then every time it tries to access it from the SD Card only rather then from Caches i guess.

I was facing the same problem when images are being downloaded from server also , and thats why i thought to store it into SD Card. but i am facing the same issue.

here is my code ListImageDownloader . In that there is a function called downloadBitmap(String) and i have created another function named downloadSDBitmap(String) whose code is as follows

Bitmap downloadSDBitmap(String urlId) {

         Bitmap bitmap = null;
         File file = new File( 
                 fileLoc +"/"+ urlId+".png");

         if(file.exists()){
             Log.d("PATH" , file.getAbsolutePath());

             bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
         }

         return bitmap;

}

apart from this the whole caching code and all are same. so can anyone help me how can i improve it as in a Gtalk android application when i scroll fast it loads the images only once after that if i scroll fast the images remains as it is and doesnt fetch from network

Update

these are my parameters

final static int MAX_ENTRIES = 150;

private static final int HARD_CACHE_CAPACITY =50;
private static final int DELAY_BEFORE_PURGE = 10 * 1000; // in milliseconds
Hunt
  • 8,215
  • 28
  • 116
  • 256

1 Answers1

1

Caching fundamentally relies on available memory. If there is memory left for your application you will need to implement a good solution that caches your bitmaps.

In the past was SoftReference/WeakReference a popular method to cache bitmaps (I did try it a year ago, you can read about my question about this here). But in later APIs of Android the garbage collector has become more aggressive collecting those and therefore they are not no longer recommended.

Now it is recommended to use an LRU cache. There is an example available on the Android developers website.

Community
  • 1
  • 1
Wroclai
  • 26,835
  • 7
  • 76
  • 67
  • i guess this requires to inclide android support package library which is unnecessary increasing the size of my application so do you have anyother solution ? – Hunt Aug 22 '12 at 07:08
  • A year ago, I was forced to write my own LRU implementation. Check the accepted answer in my question, all you need to know is written there. – Wroclai Aug 22 '12 at 08:06
  • Shelly you got your reference code ? as i am trying to integrate a code that is being suggested in your question. Do i need to replace `sSoftBitmapCache` with the code suggested ? – Hunt Aug 22 '12 at 08:59
  • Everything is written here: http://stackoverflow.com/a/224886/426429. If you want your LRU cache to have a functionality like the `LruCache` in the support package, you have to write it on your own. – Wroclai Aug 22 '12 at 09:02
  • I tried the code suggested in your question by changing soft reference to `LinkedHashMap` Now the thing is until i dont scroll a listview the images doesnt get load – Hunt Aug 22 '12 at 09:29
  • Then you have to manually invoke the method which gives you a bitmap for the specific row. – Wroclai Aug 22 '12 at 09:31
  • Well made a fix now images are loading but result is same when i was using `ConcurrentHashMap` for soft reference. here is my code `http://pastebin.com/emF4Ztxk` if you can check. I am firing ` imageDownloader.download(data.getTemplateIconUrl(), (ImageView) holder.templateIcon,true);` in a getView to fetch the image – Hunt Aug 22 '12 at 10:12
  • How big is your cache? Is there memory left for more images? Normally, when it takes some time for the image to appear, the problem is 99% because of you have to reload the images, hence your caching implementation doesn't work properly. You have to go debug your code, step by step. – Wroclai Aug 22 '12 at 11:18
  • There isn't much to tell... All you need is available - you did not want to use the support library, which means you have to write and manipulate the mechanism yourself. Debug the code! – Wroclai Aug 22 '12 at 14:46
  • i am just hitting a deadline , anyways if i use a support library then what things i should modify i mean the sample on android doc u sent here is little hard to integrate with my code so – Hunt Aug 22 '12 at 14:49
  • The article linked in my answer is an *excellent* article - it describes all you need to know. It's simple, if there is a bitmap in the cache, then get it, if it's not, then grab it from internet or the SD card and put it into the cache. It really shows everything. If it is hard to integrate you should try refactoring your code... – Wroclai Aug 22 '12 at 14:52
  • 1
    @Hunt: I have now written a demonstration application. Check it out here: https://shelly-productions.googlecode.com/files/ListViewImageCache.zip – Wroclai Aug 23 '12 at 20:30
  • 1
    For future readers, the URL is now: https://code.google.com/p/shelly-productions/source/browse/#svn%2FListViewImageCache – Wroclai Aug 27 '12 at 12:34
  • Again, for future readers, the link is now https://www.dropbox.com/s/pvr9zyl811tfeem/ListViewImageCache.zip. – Wroclai Aug 01 '14 at 20:41