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)
  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)

Tagged:

Answers

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

      String[] con=null;
      try {
        con=surface.getAssets().list("images");
        println("====================");
        println("ALL Images in assets/images");
        if (con!=null)  println(con);
        else            println("NOTHING in current assets/images");
      }
      catch(IOException e) {
        println("ERROR::NOW");
        e.printStackTrace();
      }
    

    To filter them do this:

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

    Related references:

    https://forum.processing.org/two/discussion/comment/118947/#Comment_118947
    https://developer.android.com/reference/android/content/res/AssetManager.html

    Kf

  • 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: https://forum.processing.org/two/discussion/comment/112127/#Comment_112127

    Kf

Sign In or Register to comment.