Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching SVG images on Android and memory usage

I'm using SVG Android (http://code.google.com/p/svg-android/). I'm using same svg file in several activities of my app. Is it a good idea to build a cache to store and retrieve images?

I'm using a SparseArray to store PictureDrawable (generated from SVG) in this way:

SVG svg = SVGParser.getSVGFromResource(resources, resourceId);
PictureDrawable pictureDrawable = svg.createPictureDrawable();
cache.put(resourceId, pictureDrawable);

Do I have to pay attention to memory usage when managing PictureDrawable objects? I confirm that the max items in the cache would be less than 50.

like image 653
Seraphim's Avatar asked Feb 25 '13 13:02

Seraphim's


1 Answers

Yes, its a very good Idea, Only on first run, App will generate images for that particular device screen size from svg, and store them in cache, and use these all the time after that. Saves a lot of CPU, faster UI loading.

However, I'd recommend to save cache files with names containing version of your App. If you release an update (say version 2) with some different svg images, then new files with different names will be used instead of old files.

Its normally Ok to use up to 10Mb in Context.getCacheDir(), system will clean this folder when low on storage.

Also, as a good measure, every time you initialize Cache class, you could do a little clean up, i.e. delete some old version or not requires items.

Here's a class I mostly use to just save and get a Serializable object, from App cache directory:

public class ObjectCacheFile<T> {
    private final File mFile;

    public ObjectCacheFile(Context context, String name) {
        mFile = new File(context.getCacheDir(), name);
    }

    public File getFile() {
        return mFile;
    }

    public void put(T o) {

        try {

            if (!mFile.exists()) {
                mFile.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(mFile);

            ObjectOutputStream objOut = new ObjectOutputStream(fos);

            try {
                objOut.writeObject(o);
            } finally {
                objOut.close();
            }
        } catch (IOException e) {
            Log.e(App.getLogTag(this), "error saving cache file", e);
        }
    }

    @SuppressWarnings("unchecked")
    public T get() {

        if (!mFile.exists()) {
            return null;
        }

        try {
            ObjectInputStream objIn = new ObjectInputStream(new FileInputStream(mFile));
            try {
                return (T) objIn.readObject();
            } finally {
                objIn.close();
            }
        } catch (IOException e) {
            Log.e(App.getLogTag(this), "error reading cache file", e);
        } catch (ClassNotFoundException e1) {
            Log.e(App.getLogTag(this), "cache file corrupted, deleting", e1);
            mFile.delete();
        }

        return null;
    }

}
like image 138
S.D. Avatar answered Nov 03 '22 16:11

S.D.