0

我正在开发一个 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;
}

如果您能提供帮助,我将不胜感激。谢谢。

4

2 回答 2

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;
}
于 2012-11-07T20:05:14.127 回答
0

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

于 2012-11-07T21:47:21.940 回答