2012-10-17 56 views
0
反转的位图时,我具有OutOfMemory错误

。这里是我使用反转代码:反转位图时的OutOfMemory?

public Bitmap invertBitmap(Bitmap bm) { 
     Bitmap src = bm.copy(bm.getConfig(), true); 

     // image size 
     int height = src.getHeight(); 
     int width = src.getWidth(); 
     int length = height * width; 
     int[] array = new int[length]; 
     src.getPixels(array, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight()); 
     int A, R, G, B; 
     for (int i = 0; i < array.length; i++) { 
      A = Color.alpha(array[i]); 

      R = 255 - Color.red(array[i]); 
      G = 255 - Color.green(array[i]); 
      B = 255 - Color.blue(array[i]); 

      array[i] = Color.argb(A, R, G, B); 
     } 
     src.setPixels(array, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight()); 

     return src; 
    } 

的图像是〜80 kb的大,尺寸是800x1294和图象具有话它们是黑色和一种无形的背景..
的图片都在ViewPager ..

回答

0

当你复制BM,尝试:bm = null;

0

在Android中,由于16MB(在几乎所有的手机),内存容量为应用程序,将整个位图保存在内存中并不明智。这是一种常见的情况,并且可能是开发者正在发生的。

您可以在this stackoverflow线程中获得有关此问题的许多信息。但我真的强烈要求你阅读有关Bitmap高效使用的android官方文档。它们是herehere

0

图像所使用的内存大小完全不同于该图像的文件大小。

在一个文件中,图像可能使用不同的算法(jpg,png等)进行压缩,并且当以位图的形式加载到内存中时,它会使用每个像素2或4个字节。

你的情况

所以(你没有播种的代码,但它lloks就像你正在使用每像素4个字节),每幅图像的内存大小为:

size = width * height * 4; // this is aprox 2MB 

在代码中,首先你复制将原始位图添加到新的位图,然后放置一个数组来操纵颜色。所以总共您使用的是每个图像反转的size x 3 = 6MB

有很多关于如何在Android中处理大量位图的例子,但我会离开你我的想法是最重要的议题:

  • 尽量只使用一个位图的副本在你的代码以上
  • 如果您只有图片中的文字,请使用Bitmap.Config = RGB_565。这只使用每个像素2个字节,将大小减半。在你不再需要的位图上调用recycle()
  • Bitmap.Factory有一个规模较大的选项。您可以减小仍适合您需求的图像大小。

祝你好运。