Android的"尝试使用再生位图"错误? [英] Android "Trying to use recycled bitmap" error?

查看:107
本文介绍了Android的"尝试使用再生位图"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,在Android应用程序我工作的位图。什么是假设发生的是,从网站的应用程序下载的图像,并将它们保存到设备,将其加载到内存中的位图到一个ArrayList,并将它们显示给用户。这一切工作正常,当应用程序第一次启动。但是,我已添加其中的图像被删除该用户的刷新选项,且上述程序遍布启动

I am running into a problem with bitmaps on an Android application I am working on. What is suppose to happen is that the application downloads images from a website, saves them to the device, loads them into memory as bitmaps into an arraylist, and displays them to the user. This all works fine when the application is first started. However, I have added a refresh option for the user where the images are deleted, and the process outlined above starts all over.

我的问题:通过使用刷新选项的旧图像仍然在内存中,我会很快得到OutOfMemoryError异常。因此,如果图像被刷新,我是有过ArrayList中运行,并回收旧图像。但是,当应用程序去加载新的图像到ArrayList中,它崩溃了试图用循环位图的错误。

My problem: By using the refresh option the old images were still in memory and I would quickly get OutOfMemoryErrors. Thus, if the images are being refreshed, I had it run through the arraylist and recycle the old images. However, when the application goes to load the new images into the arraylist, it crashes with a "Trying to use recycled bitmap" error.

据我了解,再造一个位图销毁位图,并释放它的内存为其他对象。如果我想再次使用位图,它必须被重新初始化。我相信,我这样做时,新文件被加载到ArrayList中事,但仍然是错误的。任何帮助是极大的pciated,因为这是非常令人沮丧的AP $ P $。问题code是如下。谢谢!

As far as I understand it, recycling a bitmap destroys the bitmap and frees up its memory for other objects. If I want to use the bitmap again, it has to be reinitialized. I believe that I am doing this when the new files are loaded into the arraylist, but something is still wrong. Any help is greatly appreciated as this is very frustrating. The problem code is below. Thank you!

public void fillUI(final int refresh) { 
// Recycle the images to avoid memory leaks
if(refresh==1) {
    for(int x=0; x<images.size(); x++)
        images.get(x).recycle();
    images.clear();
    selImage=-1; // Reset the selected image variable
}
final ProgressDialog progressDialog = ProgressDialog.show(this, null, this.getString(R.string.loadingImages));
// Create the array with the image bitmaps in it
new Thread(new Runnable() {
    public void run() {
        Looper.prepare();
        File[] fileList = new File("/data/data/[package name]/files/").listFiles();
        if(fileList!=null) {
            for(int x=0; x<fileList.length; x++) {
                try {
                    images.add(BitmapFactory.decodeFile("/data/data/[package name]/files/" + fileList[x].getName()));
                } catch (OutOfMemoryError ome) {
                    Log.i(LOG_FILE, "out of memory again :(");
                }
            }
            Collections.reverse(images);
        }
        fillUiHandler.sendEmptyMessage(0);
    }
}).start();

fillUiHandler = new Handler() {
    public void handleMessage(Message msg) {
        progressDialog.dismiss();
    }
};

}

推荐答案

您实际上并不需要在这里调用recycle方法。刷新按钮应该只是清除阵列,垃圾收集器将在稍后释放内存。如果你的内存溢出这意味着一些其他的对象仍然引用旧的图像和垃圾收集器无法将其删除。

You don't actually need to call recycle method here. Refresh button should just clear the array, garbage collector will free the memory later. If you get OutOfMemory it means some other objects are still referencing your old images and Garbage Collector can't remove them.

我可以asume一些ImageViews显示您的位图和他们保持引用的位图。你不能删除旧的位图,而他们仍然显示。所以一个可能的解决方案是清除ImageVIews太。之后,你可以清除阵列,并与新的图像填充它。

I may asume that some ImageViews display your bitmaps and they keep references to that bitmaps. You can't remove old bitmaps while they're still displayed. So a possible solution is to clear ImageVIews too. After that you can clear the array and fill it with new images.

回收释放内存,但一些ImageView的仍然显示位图,它可以这样做,在循环后,这就是为什么你得到试图用循环位图。

Recycle frees the memory but some ImageView is still displaying the bitmap and it can't do that after recycle, that's why you get "Trying to use recycled bitmap".

这些都只是一个假设,因为我看不到你的完整的code。

These all are just an assumptions because I can't see your complete code.

这篇关于Android的&QUOT;尝试使用再生位图&QUOT;错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆