user1145581 user1145581 - 4 months ago 67
Android Question

Get filenames from a directory in Android

I am trying to write an app in Android that will populate a spinner with the filenames of files found on the SD card with specific extensions. I can get it thus far to populate the spinner with the correct files, but the path is shown as well. Can anyone PLEASE tell me how to get ONLY the filename of a specific file in a directory on the SD card in Android?

Thank you

File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "path");
for (File f : yourDir.listFiles()) {
    if (f.isFile())
        String name = f.getName();
        // Do your stuff

Have a look at Environment page for more info.