We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, I want to load multiple images from a subfolder into a PImage array. In JavaMode it works without problems.
But when using Android Mode and uploading it to the phone I get a NullPointerException in line 17. So filenames probably won't be created. Why? I have no idea what to further try..
String[] filenames;
PImage[] img;
void setup() {
String imagepath = dataPath("images") + "/";
File folder = new File(imagepath);
// filter (returns true if file's extension is .jpg)
java.io.FilenameFilter pngFilter = new java.io.FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".png");
}
};
filenames = folder.list(pngFilter);
// define array size
img = new PImage[filenames.length];
for (int i = 0; i < filenames.length; i++) {
println(imagepath + filenames[i]);
img[i] = loadImage(imagepath + filenames[i]);
}
}
void draw() {
}
BUILD SUCCESSFUL in 19s 28 actionable tasks: 28 executed FATAL EXCEPTION: Animation Thread Process: processing.test.test, PID: 517 java.lang.NullPointerException: Attempt to get length of null array at processing.test.test.test.setup(test.java:37) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PSurfaceNone.callDraw(Unknown Source) at processing.core.PSurfaceNone$AnimationThread.run(Unknown Source)
Answers
https://Forum.Processing.org/two/discussion/15473/how-to-format-code-and-text
https://Forum.Processing.org/two/discussion/26878/converting-file-to-arraylist
If you place your images in data/images then you can access them via:
To filter them do this:
Related references:
https://forum.processing.org/two/discussion/comment/118947/#Comment_118947
https://developer.android.com/reference/android/content/res/AssetManager.html
Kf
Okay, thanks kfrajer. I got a filtered ArrayList of files in the subfolder called. Now the next problem is, that loadImage() can't open the files and breaks with the famous "File contains a path separator" error.
As I undestand it, the reason is android not giving permissions accessing files diretly via a path String. Is that true in way? In your example a file is opened by using this android method:
File cfile = surface.getFileStreamPath(FILENAME);
But how can loadImage() get access? It needs a string as an argument, which again results in the "path separator" error. I tried this, but it doesn't help:
PImage img_temp = loadImage(cfile.getParent());
Update: This works, but files have to be in the /data folder and not in a /data/subfolder
So for now it's very confusing and i can't find a way to load images without hardcoding every single filename.
One suggestion is to use external storage don't forget the permission). This post could provide you some ideas: https://forum.processing.org/two/discussion/comment/112127/#Comment_112127
Kf