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


#1
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


#2
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…


#3

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

https://forum.processing.org/two/discussion/21167/accessing-sd-cards-for-storage
https://forum.processing.org/two/discussion/18766/saving-loading
https://forum.processing.org/two/discussion/18126/load-image-files-from-external-storage-to-pimage
https://developer.android.com/training/data-storage/files#WriteExternalStorage

And this post is sort of a summary:

https://stackoverflow.com/questions/24474252/saving-to-externalstorage-processing-library

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

Kf