Problem Loading A Folder Full of Images

edited July 2014 in JavaScript Mode

Hello everyone, I am currently working on a project in which I need to load a folder full of images.

The snippet below runs perfectly in Java mode, but fails to render images in JS mode. I have tried for a very long time to get this working, and I was wondering why this snipped below isn't working, and if there are any other ways to load a folder full of images, without knowing the file name.

void setup()
{
for(int i = 0; i < myPNGs.length; i++)
  {
    myClasses.add(new Class(myPNGs[i]));
  }
}

PImage[] myPNGs()
 {  

   ArrayList<PImage> myImages = new ArrayList<PImage>();


   File picturesPath = sketchFile("images"); // Supposes you have a "images" folder in your sketch folder
    File[] files = picturesPath.listFiles(); // Get a list of files
    PImage[] images = new PImage[files.length];
    int imageCount = 0;

    for (int i = 0; i < files.length; i++)
    {
        String fileName = files[i].getName().toLowerCase();
        if (fileName.endsWith(".png") || fileName.endsWith(".PNG"))
        {
          images[imageCount++] = loadImage(files[i].getAbsolutePath());
        }
    }
        return images;
  }

Thanks.

Answers

  • Answer ✓

    The File class is pure Java, it won't work in JS mode, which has no means to list files on the user's drive (for security reasons).

  • Dang. I thought the problem had something to do with java specific functions/classes.

    Do you know of any other languages that work well with processingJS when it comes to passing data? I was thinking about making a PHP script to get the desired result, but I am not sure how to use that data to load images via processingJS.

    Or perhaps would loading images named "image1.png, image2.png, image3.png" etc. be possible? That way we could skip the step of having to create scrips in another language altogether.

  • If you place all your images inside subfolder "/data/", that'll work in JS Mode.
    But in order to load them, you gotta know the file name of each 1!

  • @GoToLoop. Thanks for the tip. I'll look into loading images named image1, image2, image3, etc, and just use a bat file to modify and rename the original folder of images.

  • In theory, the list of images on the server won't change, or not that often. So you can just list them in a file, and load this file with loadStrings(), to get the name of all files to load.

Sign In or Register to comment.