开放的OutOfMemoryError很多小图片的时候 [英] OutOfMemoryError when opening lot of little pictures

查看:99
本文介绍了开放的OutOfMemoryError很多小图片的时候的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android\">OutOfMemoryError:位图大小超过VM预算: - Android电子

在我的应用程序加载一些小的位图(2K和300K之间),我影响到ImageViews,最先推出我的应用程序,它工作正常时,但是当我退出,然后重新启动它,我总是一个位图中有OutOfMemoryError异常创建。

In my application I load some little Bitmap(between 2k and 300k) that I affect to ImageViews, during the first launch of my App it works fine but when I quit it and relaunch it, I always have the OutOfMemoryError during a Bitmap creation.

有人能告诉我为什么?

推荐答案

这个问题是因为你的位图的尺寸过大的比VM可以处理。例如从code,我可以看到你想要的图片粘贴到的ImageView您使用的是相机拍摄。所以一般摄像机的图像将是体积过于庞大,这将明显上升这个错误。因此,作为其他人所说,你要COM preSS图像或者通过取样,或图像转换成更小的分辨率。例如,如果你的ImageView为100×100的宽度和高度,你可以创建一个缩放位图,使您的ImageView被准确填写。您可以为做到这一点,

The problem is because your bitmap's size is too large than the VM can handle. For example from your code I can see that you are trying to paste an Image into imageView which you are capturing using Camera. So normally the camera images will be too large in size which will rise this error obviously. So as others have suggested, you have to compress your image either by sampling it or convert your image into smaller resolution. For example if your imageView is 100x100 in width and height, you can create a scaled bitmap so that your imageView gets filled exactly. You can do this for that,

Bitmap newImage = Bitmap.createScaledBitmap(bm, 350, 300,true);

和你回收你的位图和空位图这个样子,

And you have to recycle your bitmap and null your bitmap like this,

Bitmap bmap.recycle();
Bitmap bmap=null;

这篇关于开放的OutOfMemoryError很多小图片的时候的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆