2012-08-13 67 views
1

Android位图大小超过虚拟机预算。Android位图大小超过虚拟机预算错误

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

  1. 我是否需要回收我的关于活动(它包含一些imageviews和按钮和textViews)?
  2. .recycle();system.gc();之间的区别是什么?
+0

参考此答案http://stackoverflow.com/questions/11373240/android-error-java-lang-outofmemoryerror-bitmap-size-exceeds-vm - 预算/ 11373278#11373278和这到http://stackoverflow.com/questions/3823799/android-bitmap-recycle-how-does-it-work – Aerrow 2012-08-13 16:58:25

回答

2

你应该总是尝试和recycle你已经使用它们的位图。

据我了解,您应该尽量避免拨打system.gc()。 调用recycle()将允许垃圾收集位图对象。

我希望这会有所帮助。

+0

非常感谢你=) – 2012-08-13 17:10:50

+0

我做了一个新的帖子,更好的信息http://stackoverflow.com/questions/11942707/android-bitmap-size-exceeds-vm-budget-when-dealing-与位图 – 2012-08-13 21:42:41

2

尝试观看这部影片(罗曼盖伊):

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

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

+0

谢谢JoxTraex,但该VID太长:0 – 2012-08-13 17:11:15

+0

你怎么能寻求帮助,然后抱怨视频太长?你必须至少付出一些努力! – Bear 2012-08-13 17:20:49

+0

并非每个人都有无限的访问权限。我住在木棍里,通过卫星进入,我每天的上限约为400MB,所以如果有人说某件事情太大,太长了,不要这么快批评。这实际上可能是真的。 – Barak 2012-08-13 17:57:18

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; 
} 
相关问题