OutOfMemoryError:位图大小超过VM预算: - Android

时间:2010-05-28 09:49:03

标签: android bitmap out-of-memory

  

可能重复:
  Android: Strange out of memory issue while loading an image to a Bitmap object

我正在从Url下载图像并显示它们。在下载时,它正在提供out of memory error : bitmap size exceeds VM budget。我正在使用drawable。代码如下:

HttpClient httpclient= new DefaultHttpClient();
HttpResponse response=(HttpResponse)httpclient.execute(httpRequest);
HttpEntity entity= response.getEntity();
BufferedHttpEntity bufHttpEntity=new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();

Bitmap bm = BitmapFactory.decodeStream(instream);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(bm,bm.getWidth(),bm.getHeight(), true);
bm.recycle();
BitmapDrawable bt= new BitmapDrawable(useThisBitmap);
System.gc();

以下是错误:05-28 14:55:47.251: ERROR/AndroidRuntime(4188): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

7 个答案:

答案 0 :(得分:31)

decodeStream(is, outPadding, opts)

一起使用
BitmapFactory.Options opts=new BitmapFactory.Options();
opts.inDither=false;                     //Disable Dithering mode
opts.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
opts.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
opts.inTempStorage=new byte[32 * 1024]; 

答案 1 :(得分:5)

您可以检查图像大小,然后按适当因子对其进行下采样。<​​/ p>

请参阅此问题:Handling large Bitmaps

答案 2 :(得分:2)

此问题似乎已多次报告,例如herehere ...... 对不起Shalini,但如果是同样的问题,似乎根本就没有解决方案......

Romain Guy的唯一建议是to use less memory...

所以,以不同的方式思考你的东西会好运......

答案 3 :(得分:1)

最后,按照上面建议重新取样后,您可以拨打bitmap_file.recycle()

答案 4 :(得分:0)

事实是,在某些Android版本上存在BUG,特别是版本2.1始终出现类似这样的问题。

我发布了一个应用程序,其中我非常注意资源使用。我甚至删除了很多我正在使用的位图,现在它们是使用图形基元动态创建的。我还在没有使用时回收位图。当然,我已经检查过我的应用程序中没有内存泄漏:使用的内存不会在没有控制的情况下增长,它会将所有时间保持在合理的值内。

虽然我已经投入了大量精力来避免这个问题,但我仍然会遇到许多令人讨厌的例外情况,例如2.1和2.1-update1设备。我现在正在使用命令来报告崩溃,我已经看到它甚至在应用程序仅使用4兆字节的RAM时出现,比每个Android设备必须拥有的16M的堆大小小4倍 - 事实上现在大多数设备的堆大小都超过16M - 。

所有我的位图都有800x480像素的大小,在最坏的情况下,ARGB_8888每个可能不会占用超过1.5MB,但是当它只占用4兆时崩溃尝试加载一个,所以应该至少另外12 MB免费。我的大部分位图都是作为ARGB_4444加载的,占用了一半的内存,当位图看起来很差时,我只使用ARGB_8888 4444。

所以对我来说很明显,这些Android版本上有些东西不能正常工作。 99'9%的崩溃来自2.1和2.1更新,其余可能由其他准时原因解释。

答案 5 :(得分:0)

我尝试过很多可行的方法。

BitmapFactory.Options opts=new BitmapFactory.Options();
opts.inDither=false;                     //Disable Dithering mode
opts.inPurgeable=true;        
opts.inScale=8;
///after you use your images
System.gc();

答案 6 :(得分:-2)

这是一个实用的答案,我试图在运行时避免这个问题。它也解决了我的问题。

Runtime.getRuntime().gc();

调用垃圾收集器是一个好主意。