1

I want to get the names of all image file in a directory (lets say pictures) into an array of strings. I'm still new so I don't know how to approach this. I just need a way to retrieve the filenames with the .png extension from the pictures folder on the sd card so I can store it in an array.

Rakesh Sankar
  • 9,337
  • 4
  • 41
  • 66
  • 1
    [File](http://developer.android.com/reference/java/io/File.html) documentation would be a good point to start [as would searching this site or google](http://stackoverflow.com/questions/8835693/get-filenames-from-a-directory-in-android). Can you please show us what you have tried, the relevant code and the **specific** issues you are having. Read up on [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Then [edit](http://stackoverflow.com/posts/25161482/edit) your question and give all the necessary details. – RossC Sep 11 '14 at 09:58

2 Answers2

4

this is how to list files under any path.

private void listAllFiles(String pathName){
    File file = new File(pathName);
    File[] files = file.listFiles();
    if(files != null){
        for(File f : files){ // loop and print all file
            String fileName = f.getName(); // this is file name
        }
    }
}
Techfist
  • 4,314
  • 6
  • 22
  • 32
1

You can do this using the java.io.File

If you just want the names you can use.

File dir = new File("<YourPath>");
ArrayList<String> names = new ArrayList<String>(Arrays.asList(dir.list()));

If you want the whole file object use.

File dir = new File("<YourPath>");
ArrayList<File> files = new ArrayList<File>(Arrays.asList(dir.listFiles()));

More Information

dknaack
  • 60,192
  • 27
  • 155
  • 202