4

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.

Logcat:

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)

Here is my 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();                                              
}
Reno
  • 33,594
  • 11
  • 89
  • 102
naresh
  • 10,332
  • 25
  • 81
  • 124

3 Answers3

0

If you have problem with memory then you have to go wiht two options First.=> You have to decode image using Bitmap or Second => Download image and save it in temporary folder & use it. When you donnt need delete all files from folder.

For Download image and save it in folder check this link

For Decoding Check this link from stackoverflow. Maybe this will help you. Thanks

Community
  • 1
  • 1
Mr. Sajid Shaikh
  • 7,051
  • 4
  • 21
  • 35
0

On pre ICS phones the bitmaps were created on the dalvik heap and their memory cleaning was called in the finalizer which isn't a silver buller http://code.google.com/p/android/issues/detail?id=8488

Use a bitmap object and make sure call the recycle method

user213493
  • 892
  • 8
  • 10
  • I think you mean call the Bitmap.recycle method. Yup, you have to keep track of your bitmaps and remember to dispose of them properly, you can’t just throw them away like a Java object. – Lawrence D'Oliveiro Feb 07 '12 at 12:27
0

It's a common problem, and I think you should use for your bitmap a collection of SoftReference. Check the doc and online ressources for SoftReference : it will allow Android to clear the space taken by the bitmap when needed.

Raphaël Titol
  • 722
  • 4
  • 13