1

I am downloading the image from website and attach into listview.

URL aURL;
        try {
                aURL = new URL(//"http://www.orientaldaily.com.my/"+
                        imagepath[i]);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                Bitmap bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();

                imageview = (ImageView) findViewById(R.id.image_alllatestnewstitle);
                imageview.setVisibility(View.VISIBLE);
                imageview.setScaleType(ScaleType.CENTER_CROP);
                imageview.setImageBitmap(bm);
            } catch (IOException e) {
                Log.e("DEBUGTAG", "Remote Image Exception", e);
            }

When i downloading only 1 image, it got no problem, however when i downloading multiple or more than 5 images and load into listview, it cause problem.

The problem was

bitmap size exceeds VM budget

How to avoid this problem?

Note: this is not duplicate any question!

Thanks.

Alan Lai
  • 1,094
  • 7
  • 18
  • 41

2 Answers2

1

Load lot many images causes the app to run out of memory and force closes.I think this is what happening to your application.the memory issue is a complex issue android while developing an application.this can be solved by manually clearing the unused bitmaps and by using the garbage collector.

  • Try using System.gc();

  • Try recycling the bitmap using

  • Bitmap.recycle();

  • Make all the unused bitmap null.

  • Deallocate all the unused memory.

This all will help you a lot and also go through this link.Use memory analyzer it will help you spot out the Deallocated memory>try this link

public void deAllocateAllMemory() 
{
    try 
    {


        mGallery.imgLoader1.disposeImages();
        unbindDrawables(findViewById(R.id.magazineGrid));
        mGallery=null;  

        back.getBackground().setCallback(null);
        back.setOnClickListener(null);
        store.getBackground().setCallback(null);
        store.setOnClickListener(null);
        quickAction.setOnActionItemClickListener(null);
        settings.getBackground().setCallback(null);
        settings.setOnClickListener(null);
    }
    catch (Exception e) 
    {
    }

}

private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        try {
            view.getBackground().setCallback(null);
            ((BitmapDrawable) view.getBackground()).getBitmap().recycle();
            view.destroyDrawingCache();
            view.notifyAll();
        } catch (Exception e) {
        }

    }

This piece of code may help you a lil bit.

Community
  • 1
  • 1
Sreedev
  • 6,563
  • 5
  • 43
  • 66
0

Displaying Bitmaps Efficiently tutorial could help you.

amukhachov
  • 5,822
  • 1
  • 41
  • 60