Android - 在哪里存储生成的位图?
我有一个应用程序,可以动态生成 6 到 100 个小位图,供用户在给定会话中在屏幕上移动。我目前在 onCreate 中生成它们并将它们存储到 SD 卡中,以便在方向更改后我可以从外部存储中获取它们并再次显示它们。但是,这需要时间(加载),并且我想在生命周期更改之间保留位图引用,以便更快地访问。
我的问题是,是否有更好的地方来存储生成的位图?我正在考虑在我的基本活动中创建一个静态存储库,只有当应用程序完全从内存中删除(关闭、其他应用程序需要资源、30 分钟重新启动等)时才需要重新加载。
理想情况下,我希望用户能够返回到标题屏幕,单击“恢复”按钮,并且在 onCreate
中我只需访问那些驻留位图引用,而不必加载再次将它们从存储中取出。因此,我认为 Activity.onRetainNonConfigurationInstance
不是我所需要的。
或者,是否有比我正在做的或我描述的计划更好的方法来处理多个生成的位图?
I've got an app which dynamically generates anywhere from 6 to 100 small bitmaps for the user to move around the screen in a given session. I currently generate them in onCreate
and store them to the sd card, so that after an orientation change I can grab them out of external storage and display them again. However, this takes time (the loading) and I'd like to keep the bitmap references around between lifecyle changes for quicker access.
My question is, is there a better place to store my generated bitmaps? I was thinking about creating a static storage library in my base activity, something that would only need to be reloaded when the app is completely removed from memory (shutdown, other apps need resources, 30 minute restart, etc).
Ideally, I'd like the user to be able to back out to the title screen, click a "Resume" button, and in onCreate
I just have access to those resident bitmap references instead of having to load them from storage again. For this reason I don't think Activity.onRetainNonConfigurationInstance
is what I need.
Alternatively, is there a better way to handle multiple generated bitmaps than what I'm doing or the plan I described?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请务必查看 Romain Guy 关于保留的博客文章生成方向变化的位图。这听起来像是一个几乎相同的案例。其要点是,您可以使用
onRetainNonConfigurationInstance
/getLastNonConfigurationInstance
在与方向相关的活动销毁过程中传递/接收任意对象。不过,这不会帮助您退出并重新开始活动。您还可以将位图填充到类似静态缓存的对象中(可能是 上的 SoftReference HashMap Application 类),但要小心不要存储 Drawables 或 Views,这可能会导致 内存泄漏,因为它们引用了其包含的 Activity。这将在整个应用程序的生命周期中持续存在,但如果可能的话,我会避免这种情况,因为任何全局状态都可能导致丑陋的问题,并且很难在引用 Activity 实例的地方找到内存泄漏。
Definitely check out Romain Guy's blog post about retaining generated bitmaps on orientation change. This sounds like a nearly identical case. The gist of it is, you can use
onRetainNonConfigurationInstance
/getLastNonConfigurationInstance
to pass/receive an arbitrary object across orientation-related activity destruction. This won't help you for backing out and then starting over the Activity, though.You could also cram Bitmaps into a static cache-like object (possibly a SoftReference HashMap on an Application class), but be careful to not store Drawables or Views, which can cause memory leaks because of their reference to their containing Activity. This would persist for the life of the whole application, but I'd avoid that if at all possible as any global state can cause ugly problems and hard to find memory leaks where things are referencing Activity instances.