how to get the file names stored in sd card in android

sajjoo picture sajjoo · Dec 30, 2010 · Viewed 24.3k times · Source


i have a folder in sd card which contains several files. now i need to get the names of that files. can anybody have any idea how to get the file names stored in sd card?

any help will be appreciative. thanks a lot.

Answer

Valentin Rocher picture Valentin Rocher · Dec 30, 2010

Environment.getExternalStorageDirectory will give you a File corresponding to the SDCARD. Then you'll just have to use File methods.

That should be something like that :

File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "yourpath");
for (File f : yourDir.listFiles()) {
    if (f.isFile())
        String name = f.getName();
        // make something with the name
}

A little note of advice : from KitKat and above, this requires the READ_EXTERNAL_STORAGE permission.