Search code examples
androidslideshowviewflipper

How to write a dynamic Slideshow in Android


I try to write a dynamic Slideshow with ViewFlipper, but i can't put all the Bitmaps (ImageViews) into the ViewFlipper at the same time, because that would cause an OutOfMemoryException.

At the moment I have this code and it only shows the last picture and then nothing more.

mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(3000);
mViewFlipper.startFlipping();
setPictures(im);
mViewFlipper.stopFlipping();



    private void setPictures(IMI im) {
            Bitmap bmp = setView(im);
            setFlipperImage(bmp);
            bmp = null;

            mViewFlipper.setInAnimation(null);
            mViewFlipper.setOutAnimation(null);

            while (mViewFlipper.getChildCount() > 1) {
                mViewFlipper.removeViewAt(0);
            }

        }

private void setFlipperImage(Bitmap bm) {
        ImageView image = new ImageView(getApplicationContext());
        image.setImageBitmap(bm);
        mViewFlipper.addView(image);
    }

    private Bitmap setView(IMI im) {
        Bitmap bmp = null;
        try {
            bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse("file://" + im.getUri()));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return bmp;

    }

Thanks for your help in advance.


Solution

  • Don't use a ViewFlipper for this use case (for the exact reason you gave). Use a ViewPager in combination with a FragmentPagerAdapter where each Fragment shows one image. That way only max. 3 bitmaps are loaded at a time.