Search code examples
javaandroidfilesd-card

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



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.


Solution

  • 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.