简体   繁体   English

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

[英]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. 当前,我正在使用WeakHashMap来存储我的BitMap并将其链接到我的密钥,但是我注意到它开始使用大量的RAM。 Is there a better solution to caching the BitMaps till I need them? 是否有更好的解决方案来缓存BitMap,直到我需要它们为止? 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 我需要将它们保留在内存中,以防用户向后滑动到以前的视图以节省带宽并提高ViewPager的响应速度

FragmentStateAdapter is meant for precisely that. FragmentStateAdapter正是用于此目的。 Also use setOffscreenPageLimit() and setRetainInstance() in conjunction with that. 还要结合使用setOffscreenPageLimit()setRetainInstance()

Storing Bitmap objects in runtime memory is not a good idea and will sooner or later lead to OutOfMemoryError s getting thrown. 在运行时内存中存储Bitmap对象不是一个好主意,迟早会导致OutOfMemoryError

Use an LruCache . 使用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. 对于图像下载,您可以使用图像库,例如:图像加载器: https : //github.com/nostra13/Android-Universal-Image-Loader它为您提供了将图像保存在缓存中的选项。

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 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 http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html#disk-cache

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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