1

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?

UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

1 Answers1

1

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

Community
  • 1
  • 1
Fedor
  • 43,261
  • 10
  • 79
  • 89
  • I'll back Fedor on this... Coz the solution which he had given to me worked wonders... try sampling the bitmap to reduce the memory... – JaVadid Jun 15 '10 at 07:36