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();                                              
}
4
  • Is this happening on emulator or real device? Commented Feb 7, 2012 at 6:25
  • In both cases(Device and emulator). Commented Feb 7, 2012 at 6:27
  • Dont know the exact reason. I have solved this problem by giving system.gc(); . But this is not the exact way Commented Feb 7, 2012 at 6:32
  • Where should i call the gc() method. Commented Feb 7, 2012 at 6:45

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.