来自Android应用程序的错误OOM

时间:2012-11-07 19:47:19

标签: android out-of-memory

我正在开发一个Android应用程序来将一些图像加载到模拟器,但它失败并出现以下错误:

11-07 19:38:51.596: E/dalvikvm-heap(322): 73670400-byte external
allocation too large for this process.

11-07 19:38:51.596: E/GraphicsJNI(322): VM won't let us allocate
73670400 bytes

11-07 19:38:51.596: D/AndroidRuntime(322): Shutting down VM

11-07 19:38:51.616: W/dalvikvm(322): threadid=1: thread exiting with
uncaught exception (group=0x4001d800)

11-07 19:38:51.926: E/AndroidRuntime(322): FATAL EXCEPTION: main

11-07 19:38:51.926: E/AndroidRuntime(322): java.lang.OutOfMemoryError:
bitmap size exceeds VM budget

11-07 19:38:51.926: E/AndroidRuntime(322):  at
android.graphics.Bitmap.nativeCreate(Native Method)

11-07 19:38:51.926: E/AndroidRuntime(322):  at
android.graphics.Bitmap.createBitmap(Bitmap.java:468)

11-07 19:38:51.926: E/AndroidRuntime(322):  at
android.graphics.Bitmap.createBitmap(Bitmap.java:435)

我的Java代码在下面发生错误。我使用System.out.println来调试问题。

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    System.out.println("11");
    ImageView imageView = new ImageView(context);
    System.out.println("12");


    imageView.setImageResource(imageIDs[position]);
    System.out.println("13");
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    System.out.println("14");
    imageView.setLayoutParams(new Gallery.LayoutParams(150, 220));
    System.out.println("15");
    imageView.setBackgroundResource(itemBackGround);

    System.out.println("16");

    return imageView;
}

如果你能提供帮助,我真的很感激。感谢。

2 个答案:

答案 0 :(得分:1)

尝试捕获异常并在调用垃圾回收后重试

public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        System.out.println("11");
        ImageView imageView = new ImageView(context);
        System.out.println("12");
        try {

            imageView.setImageResource(imageIDs[position]);
            System.out.println("13");
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            System.out.println("14");
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 220));
            System.out.println("15");
            imageView.setBackgroundResource(itemBackGround);


    } catch (OutOfMemoryError e) {
            // run your image cache clean up here if you have one
            // call sys gc
    System.gc();        

            imageView.setImageResource(imageIDs[position]);
            System.out.println("13");
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            System.out.println("14");
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 220));
            System.out.println("15");
            imageView.setBackgroundResource(itemBackGround);
    }

        System.out.println("16");

        return imageView;
}

答案 1 :(得分:0)

显然图像文件是70兆字节,这将是你的问题。这比android应用程序允许的堆大小要大得多。