java.lang.OutOfMemoryError:位图大小超过虚拟机预算是多少? [英] java.lang.OutOfMemoryError: bitmap size exceeds VM budget?
问题描述
朋友,
我用下面的code,显示位图在屏幕上,有下一个和previous按钮来更改图像。
i am using following code to display bitmap on screen and having next and previous buttons to change images.
和获得内存不足的错误
新的code
HttpGet httpRequest = null;
try {
httpRequest = new HttpGet(mImage_URL[val]);
} catch (Exception e) {
return 0;
}
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
Bitmap bm;
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream is = bufHttpEntity.getContent();
try
{
bm = BitmapFactory.decodeStream(is);
}catch(Exception ex)
{
}
is.close();
旧code
URL aURL = new URL(mImage_URL[val]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = null;
try
{
is= conn.getInputStream();
}catch(IOException e)
{
}
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
img.setImageBitmap(bm);
和它给我的错误去codeR->德code返回false。
and it was giving me error decoder->decode return false.
在大小大于400KB的图像。
on images of size bigger than 400kb.
所以谷歌搜索后,我有了新的code的答案 老code没有给我出内存不足的错误对这些图像,但德codeR->德code返回false,所以我选用新的code。
so after googling i got new code as answer the old code was not giving me out of memory error on those images but decoder->decode return false, so i choosed new code.
任何一个指导我该如何解决,哪些是为了显示实时图像的最佳方法?
any one guide me what is the solution and which is the best approach to display live images?
推荐答案
您应该取消code与inSampleSize选项,以减少内存消耗。 <一href="http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966
You should decode with inSampleSize option to reduce memory consumption. http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966
另一种选择inJustDe codeBounds可以帮助你找到正确的inSampleSize值的http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a
Another option inJustDecodeBounds can help you to find correct inSampleSize value http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a
这篇关于java.lang.OutOfMemoryError:位图大小超过虚拟机预算是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!