0

I am using a GUI to create a slide show and I have a folder full of images(all the images are .jpg) and I want to be able to read all of them into an array list and display random pictures. I just created a self executable jar file so my friend can run the program on her computer so she doesn't have to deal with any of the code. Right now I am doing this by using an array and a for loop to read in each picture and the name of the picture is a number from 0 to size of the array,

for(int i = 0; i < pic.length-1; i++)
    {
      pic[i] = new ImageIcon(this.getClass().getResource("/Pictures/"           
     + i +".JPG")); 
    }

By using this method I have to alter the code by resizing the array then add the new picture to the folder and making sure the picture is the correct name. What I want is for my friend to be able to add a picture to the fold that the jar file uses and that would just use the new picture to the slide show but i'm not sure how to read in a picture from a folder with out knowing the name of the picture. I also want to add the pictures to an ArrayList so the size of the ArrayList will be determined by the number of pictures in the folder.

theAnon
  • 77
  • 1
  • 10
  • 3
    Have you looked at http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder – Andre Dec 10 '15 at 19:10
  • I did look at it but I didn't fully understand it. With the first answer when they System.out.println(fileEntry.getName()); is that the name of the files inside the folders? – theAnon Dec 10 '15 at 19:33
  • Note that you'll run out of memory (at some point) if you just read all images in a folder and add them to a list. For a slide show, it's probably better to load just a few images ahead, so you can make smooth transitions etc. but not wasting memory. – Harald K Dec 11 '15 at 10:37

1 Answers1

1

I modified the code a little from Read all files in a folder

Files.walk(Paths.get("C:/Users/example/pictures/")).forEach(filePath -> {
        if (Files.isRegularFile(filePath)) {
            System.out.println("File name: " + filePath.getFileName());
        }
    });

That's how you get the name of the files, you can then make them images, imageicons, or store them in an arraylist.

Community
  • 1
  • 1
Andre
  • 778
  • 1
  • 5
  • 23