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

**URL:** <https://discourse.processing.org/t/listfiles-works-in-java-mode-but-cant-get-it-working-for-android/750>\
**Category:** Processing for Android\
**Created:** [June 6, 2018, 11:08am UTC](https://discourse.processing.org/t/listfiles-works-in-java-mode-but-cant-get-it-working-for-android/750 "2018-06-06T11:08:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![helloDolly](https://avatars.discourse-cdn.com/v4/letter/h/b782af/32.png) [@helloDolly](https://discourse.processing.org/u/helloDolly)\
**Post date:** [June 6, 2018, 11:08am UTC](https://discourse.processing.org/t/listfiles-works-in-java-mode-but-cant-get-it-working-for-android/750/1 "2018-06-06T11:08:50Z")

</div>

```auto
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

---

<div class="post-metadata">

**Author:** ![helloDolly](https://avatars.discourse-cdn.com/v4/letter/h/b782af/32.png) [@helloDolly](https://discourse.processing.org/u/helloDolly)\
**Post date:** [June 6, 2018, 12:47pm UTC](https://discourse.processing.org/t/listfiles-works-in-java-mode-but-cant-get-it-working-for-android/750/2 "2018-06-06T12:47:37Z")

</div>

```auto
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…

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 6, 2018, 2:03pm UTC](https://discourse.processing.org/t/listfiles-works-in-java-mode-but-cant-get-it-working-for-android/750/3 "2018-06-06T14:03:03Z")

</div>

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

```java
  println("Hello World");

```

> [@helloDolly](#):
>
> String path = “/data/”;  
> File files = listFiles(path);  
> for (File i: files){  
> println("file: ", i);  
> }

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/21167/accessing-sd-cards-for-storage)  
> [https://forum.processing.org/two/discussion/18766/saving-loading](https://forum.processing.org/two/discussion/18766/saving-loading)  
> [Load image files from external storage to PImage - Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/18126/load-image-files-from-external-storage-to-pimage)  
> [Data and file storage overview &nbsp;|&nbsp; Android Developers](https://developer.android.com/training/data-storage/files#WriteExternalStorage)

And this post is sort of a summary:

> [java - Saving to "ExternalStorage" - Processing library - Stack Overflow](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](https://www.mobileprocessing.org/data.html#Write-Data-to-External-Storage)

Kf
