0
votes

My project is to display a slideshow of images from the selected folder.

I have done the project using ViewFlipper. The problem I am stuck at is memory issues when loading more images to the viewflipper. I get a force close error if the folder contains more than 15 images.

Bitmap mBitmap=BitmapFactory.decodeFile(ImageList.get(i));
    image.setImageBitmap(mBitmap);

    viewFlipper.addView(image);
    System.gc();

    }
    viewFlipper.startFlipping();

How can this "out of memory" error be resolved, any other workarounds? I heard somewhere that we can use a single imageview and just change the image during runtime. Any pointers as to how do I begin? I also need to set the time-interval between image changes.

EDIT: Used ImageView instead of viewflipper and now I'm able to display a single image. How do I change the image in the Imageview after say 5 seconds? Code that I implemented in onCreate is:

 ImageView imgView01 = (ImageView) findViewById(R.id.imageView1);  
            File dir = new File("/sdcard/WallpapersHD/");
             File file[]=dir.listFiles();

             for (int i=0;i<file.length;i++) {


                 Drawable d = (Drawable) Drawable.createFromPath(file[i].toString());


                 imgView01.setImageDrawable(d);
2
If you are downloading a images from server then you have apply lazylist concept in your project It will download one images at a time.RobinHood
No, my images are present in a folder in SDcard.user838522

2 Answers

1
votes

Everytime you show a new Bitmap, call recycle() on the previous image. This will free up space in memory, so you don't get the window leak error associated with bitmaps.

0
votes

If you are looking for using a single ImageView then you have to use postdelayed() and handlers().

Use this method in your onCreate(),

private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {

        imageView.removeCallbacks(mUpdateTimeTask);
        imageView.postDelayed(this, 1000);
        imageView.setBackgroudRsource(R.drawable.urImage);

    }
};

And now call these methods when required,

    imageView.removeCallbacks(mUpdateTimeTask);
    imageView.postDelayed(mUpdateTimeTask, 1000); //1 sec delay to change images