简体   繁体   中英

java.lang.OutOfMemoryError: bitmap size exceeds VM budget?

friends,

i am using following code to display bitmap on screen and having next and previous buttons to change images.

and getting out of memory error

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

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

and it was giving me error decoder->decode return false.

on images of size bigger than 400kb.

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?

You should decode with inSampleSize option to reduce memory consumption. Strange out of memory issue while loading an image to a Bitmap object

Another option inJustDecodeBounds can help you to find correct inSampleSize value http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM