Hunt
Hunt

Reputation: 8435

not accessing images from cache properly in listview

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

Upvotes: 0

Views: 180

Answers (1)

Wroclai
Wroclai

Reputation: 26925

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.

Upvotes: 1

Related Questions