Oliver Tworkowski
Oliver Tworkowski

Reputation: 191

How to load all images from folder? (Processing, Java)

EDIT: Coming back to this as a full stack developer with a masters degree is so funny. Hope it still helps somebody!

please help me. I want to load multiple images into this processing sketch without knowing the datanames. So that i can always just put .png images into the data folder and the program automatically loads them in. I've searched in some forums but didn't find anything except some code that I've already used but it doesn't run properly.

As soon as the program starts it gives me a NullPointerException at image();

This is the consoleoutput:

4096
D:\Program Files\processing-3.3.7\PROJECTS\Blendertutorial\data
[0] "1.png"
[1] "2.png"
[2] "3.png"
[3] "4.png"
[4] "5.png"
[5] "6.png"
[6] "7.png"

Also why is the folder.list(); output such a huge number? I only have 7 images in there...

import java.io.File;
String fileExtension = ".png";

java.io.File folder = new java.io.File(sketchPath("/PROJECTS/Blendertutorial/data"));
java.io.FilenameFilter extfilter = new java.io.FilenameFilter() {
  boolean accept(File dir, String name) {
    return name.toLowerCase().endsWith(fileExtension);
  }
};

PImage images;
String[] imageNames;
int i=0;
long folderInhalt = folder.length();

void setup(){
  size(500,500);
  println(folder.length());
  println(folder);
  printArray(imageNames);

  imageNames = folder.list(extfilter);
}

void draw(){
  if(mousePressed){
    images = loadImage(folder+"/"+imageNames[0]);
    println(images);
    println(imageNames[i]);
    delay(200);
    i++;
  }
  image(images,0,0);      //NULL POINTER EXCEPTION!
}

Upvotes: 2

Views: 4510

Answers (4)

Jim Matheson
Jim Matheson

Reputation: 1

I have this and it seems to work (based on the file selection example)

void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
img = loadImage(selection.getAbsolutePath());
}
}

Upvotes: 0

Kevin Workman
Kevin Workman

Reputation: 42176

why is the folder.list(); output such a huge number? I only have 7 images in there...

You aren't calling folder.list(). You're calling folder.length(). This function returns the size of the file, which is undefined for folders. More info can be found in the Java API.

Also, this is not directly related to your problem, but you should not load images inside the draw() function like that. Load them from the setup() function instead. Otherwise you'll load an image 60 times per second, which will cause your computer to catch on fire.

Moving your loading to setup() will also fix your NullPointerException problem, because you'll no longer be drawing null images when the mouse is not pressed.

Upvotes: 1

helloworld
helloworld

Reputation: 59

public void readImagesPath(String dir){
   File folder = new File(dir);
   File[] listOfFiles = folder.listFiles();

    for (File file : listOfFiles) {
      if (file.isFile()) {
         System.out.println(file.getName());
      }
    }
 }

With this code you can retrive the image paths and then you can load and draw them.

Upvotes: 0

Rann Lifshitz
Rann Lifshitz

Reputation: 4090

What happens in your code when draw() is called, but mousePressed is false?

Consider your code:

PImage images;
...
...
...

void draw(){
  if(mousePressed){
    images = loadImage(folder+"/"+imageNames[0]);
    println(images);
    println(imageNames[i]);
    delay(200);
    i++;
  }
  image(images,0,0);      //NULL POINTER EXCEPTION!
}

You have declared images but have not instantiated it.

In the case of mousePressed==false, images will remain null.

This behavior can explain your NullPointerException - you are invoking the image method with a null valued parameter.

Upvotes: 1

Related Questions