Its a little raw, but if you know you're absolutely done using a given  
bitmap object, you can call bitmap.recycle(). This immediately frees  
up the memory associated with the bitmap, and marks it as "dead",  
meaning it cannot be referenced again (i.e. don't try to draw or get/ 
set its pixels anymore).

i.e.

for (all of my big images) {
     Bitmap b = decode(...);
     canvas.drawBitmap(b, ...);
     b.recycle();
     // yikes, don't reference b again)
}

On Dec 15, 2008, at 9:04 PM, Mark K wrote:



  Is there a work around for this problem? Something I need to do
differently perhaps? It seems like garbage collection is not occurring
between successive bitmap decoding operations. Explicitly calling gc()
does not seem to help. This is particularly a problem on the G1,
because there is no way to reduce the camera resolution, all of the
camera pictures are large. If I loop through a directory of pictures
taken by the camera and use BitmapFactory.decodeFile(), I will get an
out of memory error on the 2nd or 3rd iteration. Since I am only
displaying/decoding one bitmap at a time I would hope that garbage
collection would free up the memory between operations such that this
does not occur. Any help would be greatly appreciated. Here's the code
I use: This code runs a slide show of the images in the camera
directory, it uses Handler.postDelayed() to render each picture. Is
there anyway to tweak the code to get rid of the out of memory
problem.

public boolean slideShow()
    {
        String baseDir = "/sdcard/dcim/Camera/";
        long showTime = 1500;

        File dir = new File(baseDir);
        File[] pics = dir.listFiles();
        for ( int i=0; i<pics.length; i++ )
        {
                String pic=baseDir+pics[i].getName();
                handler.postDelayed(new ShowSlide(pic), i*showTime);
        }
        return true;
    }

class ShowSlide extends Thread
    {
        String pc="";
        public ShowSlide(String pc)
        {
                this.pc=pc;
        }
        public void run()
        {
                System.out.println("Showing picture: "+pc.toString());
                displayPicture(pc);
        }
    }

public boolean displayPicture(String filepath)
    {
               File file = new File(filepath);
                BitmapFactory bfac = new BitmapFactory();
        try
        {
                bm = bfac.decodeFile(filepath);// out of memory error occurs
here!
                handler.post(new SetImage(iView, bm));
        }
        catch(Exception e)
        {
           Log.e(TAG,"display picture failed: "+filepath+" "+e.toString
());
           e.printStackTrace();
           return false;
        }

        return true;
    }

class SetImage extends Thread
    {
        ImageView vv;
        Bitmap bb;
        public SetImage(ImageView vv, Bitmap bb)
        {
                this.vv=vv;
                this.bb=bb;
        }
        public void run()
        {
                vv.setImageBitmap(bb);
        }
    }



On Dec 15, 2:48 pm, Romain Guy <romain...@google.com> wrote:
>>  I've run into this exact same problem a number of times myself. Not
>> sure if its a bug or just a limitation of the jvm, can't seem to
>> process more than a few large bitmaps without this occuring, this
>> always seems to occur when decoding bitmaps from file.
>
> Neither, you are just using too much memory.
>
> --
> Romain Guy
> Android framework engineer
> romain...@android.com
>
> Note: please don't send private questions to me, as I don't have time
> to provide private support.  All such questions should be posted on
> public forums, where I and others can see and answer them



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to