Mohammed Ismail
Mohammed Ismail

Reputation: 21

How to get the name of all image files in a folder?

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.

Upvotes: 1

Views: 3331

Answers (2)

dknaack
dknaack

Reputation: 60486

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

Upvotes: 1

Techfist
Techfist

Reputation: 4344

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

Upvotes: 4

Related Questions