listFiles() works in Java mode, but can't get it working for Android

String path =  "/data/";
File[] files = listFiles(path);
for (File i: files){
  println("file: ", i);
}

This bit of code is causing me bothers. listFiles(path) just returns null in the Android emulator instead of a list of filenames like it does in Java mode. I have the READ_EXTERNAL_STORAGE box checked in Android permissions.

Any help would be greatly appreciated.

-thx

1 Like
void setup () {
  requestPermission("android.permission.READ_EXTERNAL_STORAGE", "test");
}

void draw() {}

void test(boolean granted) {
  if (granted) {
    println("permission granted");
    
    String path =  "/data/";
    File[] files = listFiles(path);
    for (File i: files){
      println("file: ", i);
    }
  } else {
    println("permission not granted");
  }
}

I added a bit of code so I now ask for permission. It still doesn’t work…

First, please format your code. Edit post, select code and click in the </> button:

  println("Hello World");

You are trying to access a folder. Great. Where is it located? Based on your example, it is not properly defined. Since you are working with external storage, this is what I would do:

  1. Define the proper path
  2. Make sure it exists
  3. If it does, access it. Otherwise you can either throw an error/warning or create the folder yourself.

Kf

Accessing SD Cards for storage - Processing 2.x and 3.x Forum
https://forum.processing.org/two/discussion/18766/saving-loading
Load image files from external storage to PImage - Processing 2.x and 3.x Forum
Data and file storage overview  |  Android Developers

And this post is sort of a summary:

java - Saving to "ExternalStorage" - Processing library - Stack Overflow

Or I recommnd this: https://www.mobileprocessing.org/data.html#Write-Data-to-External-Storage

Kf

1 Like