2

嗨让我们说当我将活动 A 调用到活动 B 时

下面是我的代码变成

     Intent i = new Intent(TemplateList.this, PictureEditor.class);
     Bundle b = new Bundle();
     b.putString("Key", "2");
     b.putString("Index", imagepathString);
     i.putExtras(b);
     v.getContext().startActivity(i);
     System.gc();
     Runtime.getRuntime().gc();
     finish();

由于在活动 A 中加载了太多对象(太多图像),所以我通过System.gc(); 清除所有对象;Runtime.getRuntime().gc(); 将被清除并破坏活动,因此不会分配任何对象:)

活动 B 我要调用活动 A

        btnTemplate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(PictureEditor.this, TemplateList.class));
            dg.dismiss();
            if (bmp != null) {
                bmp.recycle();
                bmp = null;
            }
            System.gc();
            Runtime.getRuntime().gc();
            finish();
        }
    });

//这里我也调用System.gcRuntime.getRuntime.gc销毁活动,但不知道为什么如果activity A开始比我得到

以下错误

     12-19 12:44:48.769: E/AndroidRuntime(7539): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:405)
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:418)
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at com.redwood.PictureEditor.get_bitmap(PictureEditor.java:257)
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at com.redwood.PictureEditor$5.run(PictureEditor.java:218)
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at java.lang.Thread.run(Thread.java:1019)

任何机构都可以解决我的问题:(

4

3 回答 3

2

System.gc();手动调用or从来都不是一个好习惯Runtime.getRuntime().gc();。更好的选择是根据屏幕尺寸使用inSampleSize或可能调整图像的大小/缩放scaledBitmap()您可以在此处查看我的答案,这将使您了解如何在将图像加载到内存之前调整大小/缩放图像。

于 2012-12-19T07:47:30.050 回答
2

调用这些方法是行不通的,这些方法只是对 GC 说“如果你愿意,你现在就可以运行”。

基本上寻找那些问题:

  • 位图是不是太大了?-> 将其重新缩放到您的应用程序中所需的最小尺寸。
  • 你复制位图太多了吗?
  • 您不是通过泄漏 Activity B 的上下文来泄漏位图吗?

它可能来自很多地方,但我会先尝试将其缩小到您的应用程序中所需的最小尺寸。

于 2012-12-19T07:50:27.627 回答
1

尝试缩小图像您可以使用它

public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
    int width = image.getWidth();
    int height = image.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}

这里你可以计算你得到的图像的大小

于 2012-12-19T07:50:45.037 回答