存储位图信息的最有效方式 Android

Most Efficient Way to Store Bitmap Info Android

目前,我正在使用 Wea​​kHashMap 来存储我的 BitMap 并将其 link 存储到我的密钥中,但我注意到它开始使用大量 RAM。有没有更好的解决方案来缓存位图直到我需要它们?应用程序关闭后我不太可能需要图像,因此并不真正需要长期解决方案。

谢谢!

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() 结合那个。

在运行时内存中存储 Bitmap 对象不是一个好主意,迟早会导致 OutOfMemoryErrors 被抛出。

在开发者网站上使用 LruCache. There are plenty of online guides

It is unlikely I will need the image after the application closes, so a long-term solution isn't really needed.

将图像存储在缓存中不会成为问题,因为即使您关闭应用程序或除非您从设置中清除应用程序数据,系统也不会从缓存中清除图像。 对于图像下载,您可以使用图像库,例如: 图像加载器: https://github.com/nostra13/Android-Universal-Image-Loader 它为您提供了将图像保存在缓存中的选项。

你也可以使用 volley 库。但是在这种情况下,您必须实现自己的缓存存储代码。 http://developer.android.com/training/volley/index.html

否则你也可以实现自己的缓存代码,使用内存盘进行缓存。 http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html#disk-cache