Android, can't load multiple PImages from folder

edited March 19 in Android Mode

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) pngFilter = new {
    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( at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PSurfaceNone.callDraw(Unknown Source) at processing.core.PSurfaceNone$ Source)



  • If you place your images in data/images then you can access them via:

      String[] con=null;
      try {
        println("ALL Images in assets/images");
        if (con!=null)  println(con);
        else            println("NOTHING in current assets/images");
      catch(IOException e) {

    To filter them do this:

    if (con!=null) {
        ArrayList<String> result = new ArrayList<String>();  
        for (String strFile : con) {
          if (strFile.toLowerCase().endsWith(".png")) {
            println("Adding => "+strFile);

    Related references:


  • edited March 19

    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

    for (String strFile : filenames) {
    PImage img_temp = loadImage(strFile);
    // PImage img_temp = loadImage(dataPath(strFile)); // ... file separator error

    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:


Sign In or Register to comment.