处理大位图和 Android Activity 生命周期时出现内存不足错误
我有一个可滚动的地图应用程序,目前它有一个巨大的位图。它在启动时加载良好,但是当它失去前台状态并且用户再次将其返回时,我会收到内存不足错误。在 onPause 中,它使用 recycle 销毁位图,并将其标记为 null。 onResume 检查是否 map==null 并将再次加载位图,尽管我回收了位图,但这还是使程序崩溃......这里是一些代码。所有其他对位图映射的引用在加载/绘制之前首先检查它是否为空。
onPause
protected void onPause() {
super.onPause();
Log.e("sys","onPause was called");
if (map != null)
{
map.recycle();
map = null;
System.gc();
Log.e("sys","trashed the map");
}
}
我的 onResume
protected void onResume(){
super.onResume();
Log.e("sys","onResume was called");
if (map == null)
map = BitmapFactory.decodeResource(getResources(),
R.drawable.lowresbusmap);
Log.e("sys","redrew the map");
}
I have a scrollable map app which for now has a huge bitmap. It loads fine on startup, but when it looses foreground status and the user brings it backs again im getting an out of memory error. In onPause it trashes the bitmap using recycle, and marks it as null. The onResume checks to see if map==null and will load the bitmap back again, which is crashing the program despite me recycling the bitmap...Here are some bits of code. All of the other references to Bitmap map first check if it is null before loading/drawing.
onPause
protected void onPause() {
super.onPause();
Log.e("sys","onPause was called");
if (map != null)
{
map.recycle();
map = null;
System.gc();
Log.e("sys","trashed the map");
}
}
my onResume
protected void onResume(){
super.onResume();
Log.e("sys","onResume was called");
if (map == null)
map = BitmapFactory.decodeResource(getResources(),
R.drawable.lowresbusmap);
Log.e("sys","redrew the map");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这样:
Try it this way: