1

Possible Duplicate:
Android: out of memory exception in Gallery

In my application i am loading images from the internet. It loads successfully but some times i am getting the "OUT OF MEMORY EXCEPTION" How to handle this? can any body help me. I am putting the download process in the thread. refer the following code.

public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder)
{
                               nImageDownLoads++;

                System.out.println("The images being downloaded :" + nImageDownLoads);



                 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
                        {
                            Item.bImageDownLoaded = 2;
                            System.out.println("Downloading image : " + Item.ImageUrl);
                            InputStream is = fetch(Item.ImageUrl);
                            Drawable drawable = Drawable.createFromStream(is, "src");
                            //is.close();
                            //is = null;

                            nImageDownLoads--;
                            System.out.println("Downloaded image :" + Item.ImageUrl);
                            System.out.println("Remaining images for downloading: " + nImageDownLoads);
                             if(drawable != null)
                             {
                                Item.dwgImage = drawable;   

                                Item.bImageDownLoaded = 1;

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

                                //drawable = null;
                             }
                             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());
                        }
                    }                                               
                };
                t.start();  
            }
private InputStream fetch(String urlString) throws MalformedURLException, IOException 
{         
                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        HttpGet request = new HttpGet(urlString);
                    HttpResponse response = httpClient.execute(request);
               return response.getEntity().getContent();
} 
Community
  • 1
  • 1
naresh
  • 10,332
  • 25
  • 81
  • 124
  • Check out these Answers buddy. [Answer 3](http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue) [Answer 2](http://stackoverflow.com/questions/4544919/android-how-to-handle-out-of-memory-exception) [Answer 1](http://stackoverflow.com/questions/3238388/android-out-of-memory-exception-in-gallery) – Lalit Poptani Sep 01 '11 at 05:13

0 Answers0