0

我有人在我的应用程序上加载一堆图像时出错,我不确定是什么原因造成的。

这是他得到的错误。LINE 135 在底部。
我认为他的手机内存不足。他使用的是 2010 年制造的 Desire HD。此方法的作用是从服务器获取图像并将其保存在哈希图中。随着将其显示在屏幕上。关于如何防止这种 OUTMEMORYERROR 的任何想法?

我正在考虑设置一个计数器,如果有超过 20 个图像,它只会清除哈希图。

日志猫:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
at com.codalata.craigslistchecker.GETTHEIMAGE$ITask.doInBackground(GETTHEIMAGE.java:135)
at com.codalata.craigslistchecker.GETTHEIMAGE$ITask.doInBackground(GETTHEIMAGE.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

代码:

 public class GETTHEIMAGE {

private HashMap<String, Drawable> ICache;
private static Drawable DefIcon = null;
private BaseAdapter adapt;
private ItemView item;
private int num = 0;

public GETTHEIMAGE(Context c) {
    ICache = new HashMap<String, Drawable>();
}

public Drawable GetTheImagedata(
        com.codalata.craigslistchecker.Favorites.TheListAdapter theListAdapter,
        ImageView ICON_IMG) {
    this.adapt = theListAdapter;
    String url = (String) ICON_IMG.getTag();
    if (ICache.containsKey(url)) {
        return ICache.get(url);
    } else {
        new ITask().execute(url);
        return DefIcon;
    }
}

public Drawable GetTheImagedata(TheListAdapter adapter, ImageView ICON_IMG) {
    this.adapt = adapter;
    String url = (String) ICON_IMG.getTag();
    if (ICache.containsKey(url)) {
        return ICache.get(url);
    } else {
        new ITask().execute(url);
        return DefIcon;
    }
}

public Drawable GetTheImagedata2(ItemView itemView, String string) {
    this.item = itemView;
    String url = string;
    if (ICache.containsKey(url)) {
        return ICache.get(url);
    } else {
        new ITask2().execute(url);
        return DefIcon;
    }
}

private class ITask2 extends AsyncTask<String, Void, Drawable> {
    private String Nick;

    @Override
    protected Drawable doInBackground(String... params) {
        Nick = params[0];
        InputStream isImage = null;
        try {
            URL url = new URL(Nick);
            isImage = url.openStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Drawable.createFromStream(isImage, "Nick");
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        if (num == 0) {
            item.ivItemimage1.setImageDrawable(result);
        }
        if (num == 1) {
            item.ivItemimage2.setImageDrawable(result);
        }
        if (num == 2) {
            item.ivItemimage3.setImageDrawable(result);
        }
        if (num == 3) {
            item.ivItemimage4.setImageDrawable(result);
        }
        if (num == 4) {
            item.ivItemimage5.setImageDrawable(result);
        }
        if (num == 5) {
            item.ivItemimage6.setImageDrawable(result);
        }
        if (num == 6) {
            item.ivItemimage7.setImageDrawable(result);
        }
        if (num == 7) {
            item.ivItemimage8.setImageDrawable(result);
        }
        if (num == 8) {
            item.ivItemimage9.setImageDrawable(result);
        }
        if (num == 9) {
            item.ivItemimage10.setImageDrawable(result);
        }
        num++;
    }
}

private class ITask extends AsyncTask<String, Void, Drawable> {
    private String Nick;

    @Override
    protected Drawable doInBackground(String... params) {
        Nick = params[0];
        InputStream isImage = null;
        try {
            URL url = new URL(Nick);
            isImage = url.openStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try{
 LINE 135 ----->            return Drawable.createFromStream(isImage, "Nick");
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        synchronized (this) {
            ICache.put(Nick, result);
        }
        adapt.notifyDataSetChanged();
    }
}
 }
4

2 回答 2

1

您需要缩小位图并在内存中加载缩小的版本。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

根据需要使用适当BitmapFactory.decode的方法。

这是一个例子:

位图内存不足错误

注意:从 Android 3.0(API 级别 11)开始,像素数据与关联的位图一起存储在 Dalvik 堆中。

在 Android 2.3.3(API 级别 10)及更低版本recycle()上,建议使用。如果您在应用程序中显示大量位图数据,您可能会遇到 OutOfMemoryError 错误。recycle() 方法允许应用程序尽快回收内存。

Android 3.0(API 级别 11)引入了该BitmapFactory.Options.inBitmap字段。如果设置了此选项,则采用 Options 对象的解码方法将在加载内容时尝试重用现有位图。这意味着位图的内存被重用,从而提高了性能,并消除了内存分配和取消分配。

http://developer.android.com/training/displaying-bitmaps/manage-memory.html

于 2013-07-20T06:38:09.590 回答
0

在您的第一个 AsyncTask 类中num++;总是增加,因此在某个值上它超出了为该程序分配的内存空间,您应该对此进行控制

于 2013-07-20T06:45:41.520 回答