【问题标题】:Android bitmap size exceeds VM budget errorAndroid 位图大小超出 VM 预算错误
【发布时间】:2012-08-09 22:34:10
【问题描述】:

Android 位图大小超出 VM 预算。

我的应用经常出现此错误。我有两个问题。

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

【问题讨论】:

标签: android bitmap recycle


【解决方案1】:

试试看这个视频(罗马人):

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

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

【讨论】:

  • 你怎么能寻求帮助然后抱怨视频太长???你至少要付出一些努力!
  • 不是每个人都可以无限制访问。我住在棍子里并通过卫星访问,在那里我每天的上限约为 400 MB,所以如果有人说某些内容太大、太长等等,请不要这么快批评。这可能是真的。
【解决方案2】:

你应该总是尝试recycle位图在你使用它们之后。

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

我希望这会有所帮助。

【讨论】:

【解决方案3】:

我在从相机中挑选图像时遇到了同样的问题。
我使用以下代码调整了图像位图的大小:

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;
}

【讨论】:

    猜你喜欢
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2010-12-07
    • 2010-12-29
    • 1970-01-01
    相关资源
    最近更新 更多