Wednesday, July 13, 2011

Bitmap size out of memory

Today we are discuss about the how to handle the bitmap size exceed exception.

The main problem for bitmap is, the memory size allocation for the bitmap is in native heap and the recycler of the native heap is to lazzy.

So whenever we deal with the large amount of images just recycle the bitmap object by manually.

I used the following way for my project to display images.

try
 {
       if(bm!=null)
         {
            bm.recycle();   //here we recycle the bitmap object for further use.
                                   // you can also use System.gc();
            bm=null;
          }

       InputStream stream;
       stream = new FileInputStream("your image file path");
      
       bm = BitmapFactory.decodeStream(stream, null, null);

       try
         {
           stream.close();
           stream=null;
          }
       catch (IOException e)
        {
         // TODO Auto-generated catch block
          e.printStackTrace();
        }

       imgView.setImageBitmap(bm);
   }
   catch (FileNotFoundException e)
   {
     // TODO Auto-generated catch block
      e.printStackTrace();
   }

Hope this will help you.

If you find better way then this please shared it.