OutOfMemoryError:位图大小超出 VM 预算:- Android
我正在从 Url 下载图像并显示它们。在下载时它给出 内存不足错误:位图大小超出 VM 预算
。 我正在使用可绘制的。代码如下:
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`
Possible Duplicate:
Android: Strange out of memory issue while loading an image to a Bitmap object
i am downloading images from Url and displaying them. At download time it is givingout of memory error : bitmap size exceeds VM budget
.
I am using drawable. Code is below:
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();
Here is the error:
`05-28 14:55:47.251: ERROR/AndroidRuntime(4188):
java.lang.OutOfMemoryError: bitmap size exceeds VM budget`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
decodeStream(is, outPadding, opts)
与Use
decodeStream(is, outPadding, opts)
with您可以检查图像大小,然后按适当的因子对其进行下采样。
请参阅此问题:处理大位图
You could check the image size and then downsample it by appropriate factor.
See this question: Handling large Bitmaps
此问题似乎已被报告过多次,此处 和此处例如...
抱歉,Shalini,但如果是同样的问题,似乎根本没有解决方案......
Romain Guy 的唯一建议是 使用更少的内存...
所以,祝你好运,以不同的方式思考你的东西...
This issue seems to have been reported several times, here and here for instance...
sorry Shalini but if it's the same issue, it seems that there is no solution at all...
The only advice of Romain Guy is to use less memory...
So, good luck to think your stuff differently...
最后,按照上面的建议重新采样图像后,您可以调用 bitmap_file .recycle()。
Finally, after resample the image as suggested above, you may call bitmap_file.recycle().
事实是,某些 Android 版本存在 BUG,尤其是 2.1 版本,总是出现这样的问题。
我发布了一个应用程序,其中我非常注意资源的使用。我什至删除了我正在使用的许多位图,现在它们是使用图形基元动态创建的。当不使用位图时,我也会回收它们。当然,我已经检查过我的应用程序中没有内存泄漏:使用的内存不会在不受控制的情况下增长,它始终将时间保持在合理的值内。
尽管我投入了大量精力试图避免这个问题,但我仍然收到许多恼人的异常,例如来自 2.1 和 2.1-update1 设备的异常。我现在使用标准来报告崩溃,并且我发现即使应用程序仅使用 4 MB RAM(比每个 Android 设备应用程序必须拥有的 16M 堆大小小四倍)也会发生这种情况 - 事实上现在大多数设备的堆大小都大于 16M。
我所有位图的大小都是 800x480 像素,在最坏的情况下,ARGB_8888 可能不会占用超过 1.5MB,但是当只占用 4 MB 时,尝试加载一个位图会崩溃,所以应该至少还有 12 MB自由的。我的大多数位图都以 ARGB_4444 的形式加载,占用了一半内存,只有当位图在 4444 下看起来很糟糕时,我才使用 ARGB_8888。
所以对我来说,很明显这些 Android 版本上有一些东西不能正常工作。 99'9% 的崩溃来自 2.1 和 2.1-update,其余的可能是其他原因造成的。
The FACT is that there is a BUG on some Android versions, particularly version 2.1 fails all the time with issues like this one.
I released an app in which I have took a lot of care on resource use. I have even deleted a lot of bitmaps that I were using and now they are created on the fly using graphic primitives. I also recycle bitmaps when no using them. And of course I have checked that I have no memory leaks in my app: the used memory does NOT grow without control, it keeps all the time within reasonable values.
Although I have invest a lot of effort on trying to avoid this problem, I continue getting lots of annoying exceptions like that from 2.1 and 2.1-update1 devices. I am using critercism now to report crashes, and I have seen that It occurs even when the app is using just 4 megabytes of RAM, four times less than the 16M of heap size that every Android device must have for an app -and the fact is that most of devices these days have heap sizes bigger than 16M-.
All my bitmaps have a size of 800x480 pixels, that on the worst case as ARGB_8888 may not occupy more than 1.5MB each one, but it crashes trying to load one when having just 4 megabytes occupied, so there should be at least another 12 MB free. And most of my Bitmaps are loaded as ARGB_4444 that occupies a half of memory, I only use ARGB_8888 when the bitmaps looks really poor with 4444.
So for me it is pretty clear that there is something on these Android versions that is not working fine. 99'9% of this crashes comes from 2.1 and 2.1-update, and the rest may be explained by other punctual reasons.
我尝试了很多事情,这就是有效的。
I've tried many things this is what works.
这是一个实用的答案,我试图在运行时避免这个问题。而且它也解决了我的问题。
调用垃圾收集器是个好主意。
This is a practical answer, what I tried to avoid this problem at Run-time. And it also solved my problem.
Calling Garbage Collector is a good Idea.