简体   繁体   中英

Most Efficient Way to Store Bitmap Info Android

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!

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.

Use an LruCache . There are plenty of online guides on the developer site.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM