android - 内存不足的问题

时间:2011-09-12 08:30:29

标签: android

在我的应用中,我正在从网上下载图片。为此,我第一次从网上下载图像,这些图像存储在SD卡中。下次,我正在检查这些图像是否在SD卡中。如果是的话,从SD卡获取,否则我从网上下载。这些图像显示为列表。我反复(意味着连续向上/向下移动)滚动列表然后我的应用程序崩溃,我得到内存不足异常。如何处理它。有谁能够帮我。

堆栈跟踪:

09-12 13:40:42.640: ERROR/AndroidRuntime(3426): java.lang.OutOfMemoryError: bitmap size exceeds VM  budget(Heap Size=7879KB, Allocated=3362KB, Bitmap Size=11402KB)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  android.graphics.BitmapFactory.nativeDecodeFile(Native Method)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:355)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:433)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  com.ibkr.elgifto.GiftCategories$itemlistadapter$4.getDrawableFromUrl(GiftCategories.java:830)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  com.ibkr.elgifto.GiftCategories$itemlistadapter$4.run(GiftCategories.java:739)
09-12 13:53:32.450: INFO/WSP(332): [Receiver] next auto-sync alarm event is 180 mins later, at time: Mon Sep 12 16:53:32 GMT+05:30 2011

private Drawable getDrawableFromUrl(String imageUrl) {

    // TODO Auto-generated method stub
    String filename = imageUrl;
    filename = filename.replace("/", "+");
    filename = filename.replace(":", "+");
    filename = filename.replace("~", "s");
    File elgiftoimagesref = new File("/sdcard/Elgiftoimages/");

    elgiftoimagesref.mkdir();

    final File file = new File(elgiftoimagesref, filename);
    BitmapDrawable SDdrawable = null;

    boolean exists = file.exists();
    if (!exists) {
        try {
            URL myFileUrl = new URL(imageUrl);
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            final Bitmap result = BitmapFactory.decodeStream(is);
            is.close();
            new Thread() {
                public void run() {
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    if (result != null) {
                        result.compress(Bitmap.CompressFormat.JPEG, 40,
                                bytes);
                    }
                    try {

                        FileOutputStream fo;
                        fo = new FileOutputStream(file);
                        fo.write(bytes.toByteArray());

                        fo.flush();
                        fo.close();
                        // result.recycle();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            BitmapDrawable returnResult = new BitmapDrawable(result);
            return returnResult;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    } else {

        // Here i am getting the out of memory error.
        SDdrawable = new BitmapDrawable(BitmapFactory.decodeFile(file
                .toString()));
        return SDdrawable;
    }
}       

1 个答案:

答案 0 :(得分:0)

我认为您没有将 ViewHolder模型用于listview。在这种情况下,您应该得到这样的错误,因为所有列表项都将在内存中可用,无论它是否可见。使用ViewHolder内存仅分配给那些可见且当前在屏幕上的项目。 ViewHolder模式将性能提高150% 有关ViewHolder的视频教程,请查看this。如果您已经实现,那么内存泄漏的情况可以参考this