1

I am beginner to android. I want to use Memory Cache in my application to store images in memory cache and clear it when those images are not required to be in cache! Any help will be greatly appreciated.

Manoj
  • 2,799
  • 5
  • 30
  • 49
  • I have tried with Disk Cache. ViewPager is not picking images properly from Disk cache. But it does from Memory cache i have tried these things from [Processing Bitmpas off the Ui thread](http://developer.android.com/training/displaying-bitmaps/process-bitmap.html) and [Caching bitmaps](http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html) but as i am beginner i have not used it properly so app is still getting crashed with OutOfMemory. Can you help me with Processing Bitmaps Off the UI thread with Memory Cache? Thanks in advance! – Manoj Dec 31 '12 at 06:43

3 Answers3

2

You can use WeakHashMap to implement a simple caching method. It's elements will be garbage collected, so if your application uses up its heap, then it will be freed, but until that you can have the images in the memory.

András Kerekes
  • 1,001
  • 7
  • 13
  • thanks @András Kerekes ! can you lead to some tutorial? What if i have to use it with ViewPager? Will that be efficient? I have around 36 images which have resolution around 1280X800. App crashed on XHDPI devices saying OutOfMemory error.VM wont let you allow to allocates some byte of memory!. – Manoj Dec 31 '12 at 06:12
  • Unfortunately I am not that much into Android, so I don't know how does this work with ViewPager. The solution I suggested you is a rather general one. You define one WeakHashMap and make it visible for the views that display the images. When you need an image, you check tha map first, and if it does not have it, you load the image and put into the hashmap. If later you'll need the same image (and GC has not cleaned it up) you can access it from the map. – András Kerekes Dec 31 '12 at 06:18
0

Please use the following code and let me know .

public class MyApplication extends Application {
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String s : children) {
                if (!s.equals("lib")) {
                    deleteDir(new File(appDir, s));
                    Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
                }
            }
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }
}
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
  • I have tried with Disk Cache. ViewPager is not picking images from Disk cache. But it does from Memory cache i have tried these things from [Processing Bitmpas off the Ui thread](http://developer.android.com/training/displaying-bitmaps/process-bitmap.html) and [Caching bitmaps](http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html) but as i am beginner i have not used it properly so app is still getting crashed with OutOfMemory. Can you help me with Processing Bitmaps Off the UI thread with Memory Cache? Thanks in advance! – Manoj Dec 31 '12 at 06:24
0

Memory caching is a function of java, not Android.

import java.util.*

Then use List, Map, ArrayList and HashMap to build a name/value paired array of memory mapped objects and store it as a hashmap. You can then access (read/delete/update) objects in memory via the hashmap.

If you need to save the memory objects for future instantiations (e.g. through application stop and start), only then do you need to write the data to external storage (presumably, images and all), and read it again, as needed.

However, in most cases, you are building the local memory cache because you have just retrieved the data and would like other classes to process it now or in the near future. If your application cannot find data because it restarted, or it is no longer in memory because of memory constraints, it's best to retrieve the data again and not rely on potentially stale cache files.

Andrew
  • 906
  • 7
  • 9