Android位图大小超过VM预算错误

时间:2012-08-13 16:46:55

标签: android bitmap recycle

Android位图大小超过VM预算。

我的应用经常收到此错误。我有两个问题。

  1. 我是否需要回收我的约会活动(它包含一些图像视图和按钮以及textViews)?
  2. .recycle();system.gc();之间有什么区别?

3 个答案:

答案 0 :(得分:2)

尝试观看此视频(罗曼家伙):

http://www.youtube.com/watch?v=duefsFTJXzc&list=PLD1B287286E23E2D1&index=1&feature=plpp_video

它将为位图的最佳实践提供一些见解。

答案 1 :(得分:2)

在使用它们之后,您应该尝试recycle位图。

据我了解,您应该尽量避免致电system.gc()。 调用recycle()将允许对位图对象进行垃圾回收。

我希望这会有所帮助。

答案 2 :(得分:0)

从相机中拾取图像时遇到了同样的问题 我使用以下代码调整了图像的位图:

Bitmap bitmap = resizeBitMapImage(picturePath, 75, 91);
            profilePic.setImageBitmap(bitmap);

private Bitmap resizeBitMapImage(String filePath, int targetWidth,
        int targetHeight) {

    Bitmap bitMapImage = null;
    // First, get the dimensions of the image
    Options options = new Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    double sampleSize = 0;
    // Only scale if we need to
    // (16384 buffer for img processing)
    Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
            .abs(options.outWidth - targetWidth);

    if (options.outHeight * options.outWidth * 2 >= 1638) {
        // Load, scaling to smallest power of 2 that'll get it <= desired
        // dimensions
        sampleSize = scaleByHeight ? options.outHeight / targetHeight
                : options.outWidth / targetWidth;
        sampleSize = (int) Math.pow(2d,
                Math.floor(Math.log(sampleSize) / Math.log(2d)));
    }

    // Do the actual decoding
    options.inJustDecodeBounds = false;
    options.inTempStorage = new byte[128];
    while (true) {
        try {
            options.inSampleSize = (int) sampleSize;
            bitMapImage = BitmapFactory.decodeFile(filePath, options);

            break;
        } catch (Exception ex) {
            try {
                sampleSize = sampleSize * 2;
            } catch (Exception ex1) {

            }
        }
    }
     return bitMapImage;
}
相关问题