【问题标题】:java.lang.OutOfMemoryError: bitmap size exceeds VM budget?java.lang.OutOfMemoryError:位图大小超出 VM 预算?
【发布时间】:2010-06-15 07:10:00
【问题描述】:

朋友们,

我正在使用以下代码在屏幕上显示位图,并使用下一个和上一个按钮来更改图像。

内存不足错误

新代码

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(); 

旧代码

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);

它给了我错误解码器->解码返回错误。

在大小大于 400kb 的图像上。

所以在谷歌搜索后我得到了新代码作为答案 旧代码在这些图像上没有给我内存不足错误,但解码器->解码返回 false,所以我选择了新代码。

任何人指导我什么是解决方案,哪种是显示实时图像的最佳方法?

【问题讨论】:

    标签: android


    【解决方案1】:

    您应该使用 inSampleSize 选项进行解码以减少内存消耗。 Strange out of memory issue while loading an image to a Bitmap object

    另一个选项 inJustDecodeBounds 可以帮助您找到正确的 inSampleSize 值http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a

    【讨论】:

    • 我会支持 Fedor 的...因为他给我的解决方案效果很好...尝试对位图进行采样以减少内存...
    猜你喜欢
    • 2012-08-19
    • 2011-04-26
    • 2010-12-29
    • 2011-07-27
    相关资源
    最近更新 更多