3

My application is using bitmaps and every time the user come to the specific activity where it shows an image the second time it stops working.

Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"//Pics/"Image.jpg"); 

I have tried using things like...

BitmapFactory.Options options = new BitmapFactory.Options(); options.inTempStorage = new byte[16*1024]; 

Not sure what to set it too. But this doesnt help. Once the user leaves this activity is there not a way to clear the bitmap etc? thanks

3 Answers 3

8

Call Bitmap.recycle() when you are done using the Bitmap to free the memory.

Sign up to request clarification or add additional context in comments.

Comments

8

Besides using Bitmap.recycle() as suggested (which is not suitable for all situations and it's a pain in the neck to be asking: "do I still need this bitmap?"), I always use this technique which works really fine:

// 1. create a cache map private WeakHashMap<String, SoftReference<Bitmap>> mCache; 

As you can see, it's a hash map of WeakReferences with a SoftReference as the values.

//2. when you need a bitmap, ask for it: public Bitmap get(String key){ if( key == null ){ return null; } if( mCache.containsKey(key) ){ SoftReference<Bitmap> reference = mCache.get(key); Bitmap bitmap = reference.get(); if( bitmap != null ){ return bitmap; } return decodeFile(key); } // the key does not exists so it could be that the // file is not downloaded or decoded yet... File file = new File(Environment.getExternalStorageDirectory(), key); if( file.exists() ){ return decodeFile(key); } else{ throw new RuntimeException("Boooom!"); } } 

This will check the cache map. If the file was already decoded, it will be returned; otherwise it will be decoded and cached.

//3. the decode file will look like this in your case private Bitmap decodeFile(String key) { Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"//Pics/"+key); mCache.put(key, new SoftReference<Bitmap>(bitmap)); return bitmap; } 

Working with soft references is nice because you shift the responsibility of removing bitmaps from memory to the OS.

1 Comment

After all those years, do you still use this method? If so, would you please tell why you still use SoftReference while you already have a WeakHashMap?
1

Be aware. When we think about softreferences we think that the OS will remove the softreferenced objects from memrory before reporting an outofmemory exception.

In android this is not always true. I had to implement my own caching system for images and I can assure you softreferenced objects were not removed from memory when memory was almost full.

Finally I had to switch to hard references (the normal ones) but used android.support.v4.util.LruCache for managing the cached objects. I would call recycle on the onRemoved callback from the lru cache. Its definetely more convenient.

Cheers.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.