Reputation: 754
Currently, I am using a WeakHashMap to store my BitMap and link it to my key, but I have noticed it begins to use a ton of RAM. Is there a better solution to caching the BitMaps till I need them? It is unlikely I will need the image after the application closes, so a long-term solution isn't really needed.
Thanks!
Upvotes: 1
Views: 926
Reputation: 135
It is unlikely I will need the image after the application closes, so a long-term solution isn't really needed.
Storing images in cache wont be a problem as system doesn't clear image from cache even after you close the application or unless you clear the app data from setting. For image downloading you can use image libraries like: Image loaders: https://github.com/nostra13/Android-Universal-Image-Loader It gives you option to save image in cache.
You can also use volley library. But in this you have to implement your own code for cache storing. http://developer.android.com/training/volley/index.html
Otherwise you can also implement your own code for cache in which you can use memory disk for caching. http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html#disk-cache
Upvotes: 0
Reputation: 8870
Use an LruCache. There are plenty of online guides on the developer site.
Upvotes: 0
Reputation: 30611
I need to keep them in memory in case the user swipes back to a previous view to save bandwidth and make the ViewPager more responsive
FragmentStateAdapter
is meant for precisely that. Also use setOffscreenPageLimit()
and setRetainInstance()
in conjunction with that.
Storing Bitmap
objects in runtime memory is not a good idea and will sooner or later lead to OutOfMemoryError
s getting thrown.
Upvotes: 2