Search code examples
javaandroidandroid-imageviewandroid-image

showing multiple images in imageviewer through loop


I want to show multiple images in my imageView I want to do this by using a for loop, but my image view only shows the last image.

I have tried Thread.sleep and sleep functions too but they didn't work

for(int i=0;i<=3;i++){
                try {

                    pic = picText.getText().toString();
                    String photoPath = "sdcard/Pictures/" + pic + i + ".jpg";
                    imageView.setImageBitmap(BitmapFactory.decodeFile(photoPath));
                    sleep(1000);
                } catch (Exception ex) {

                }}

I expect to get all the images one after other, the delay is given in sleep() function.


Solution

  • Get all the imagepath in array or ArrayList and then use a handler and timertask

    Timer t; Handler handler; Runnable update; handler = new Handler(); update = new Runnable() { @Override public void run() {

                //load photo
            }
        };
        t = new Timer();
               t.schedule(new TimerTask() {
                   @Override
                   public void run() {
                       handler.post(update);
                   }
               }, 0, 5000);
    
    Note if its showing last image, redirect counter to 1st image. Hope this helps