How to avoid the out of memory exception
<p>In My application i am downloading the images from the web using url of the image. I have lot of images So i implemented the paging technique for this and i displayed 15 images for each page in vertical order. In this case i am scrolling up/down the page to view the images at this time my app is crashed and i got out of memory exception. please can anybody help me.</p>

<p>Logcat:</p>

<pre><code>02-07 11:23:52.256: ERROR/ACRA(7236): El Gifto fatal error : bitmap size exceeds VM budget(Heap Size=7943KB, Allocated=3485KB, Bitmap Size=12546KB)    
02-07 11:23:52.256: ERROR/ACRA(7236): java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7943KB, Allocated=3485KB, Bitmap Size=12546KB)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:690)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:490)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at com.ibkr.elgifto.GiftCategories$itemlistadapter$3.getDrawableFromUrl(GiftCategories.java:837)    
02-07 11:23:52.256: ERROR/ACRA(7236):  at com.ibkr.elgifto.GiftCategories$itemlistadapter$3.run(GiftCategories.java:724)
</code></pre>

<p>Here is my code:</p>

<pre><code>public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder)
{   
    final Handler handler = new Handler() 
    {             
        @Override  public void handleMessage(Message message) 
        {                 
            holder.imgitem.setImageDrawable((Drawable) message.obj);   
            holder.imgitem.setVisibility(View.VISIBLE);
            holder.progress.setVisibility(View.GONE);
        }
     };

     //Thread for getting the attributes values
     Thread t = new Thread() 
     {
        public void run()
        {                       
            try
            {                                       
                drawable = getDrawableFromUrl(Item.ImageUrl);                                                       

                if(drawable != null)
                {                                                               
                    //Send the message to the handler
                    Message message = handler.obtainMessage(1, drawable);                 
                    handler.sendMessage(message);                                                               
                }
                else
                {
                    int idNoImage = R.drawable.giftsuggestionsnoimage;
                    Drawable dwgNoImg = GiftCategories.this.getResources().getDrawable(idNoImage);

                    //Send the message to the handler
                    Message message = handler.obtainMessage(1, dwgNoImg);                 
                    handler.sendMessage(message); 
                }                        

            }
            catch(Exception exp)
            {
                System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage());
            }
        }

        private Drawable getDrawableFromUrl(String imageUrl) throws IOException
        {                   
            Drawable image = null;

            try 
            {
                InputStream in = (java.io.InputStream) new java.net.URL(imageUrl).getContent();
                if (in != null)
                {
                    image = Drawable.createFromStream(in, "image");
                    in.close();
                }
            } 
            catch (Exception ex) 
            {
                ex.printStackTrace();
            }
            return image;
        }                                                               
    };
    t.start();                                              
}
</code></pre>
