Skip to content Skip to sidebar Skip to footer

Get Filenames From A Directory In Android

I want to 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 th

Solution 1:

FilesdCardRoot= Environment.getExternalStorageDirectory();
FileyourDir=newFile(sdCardRoot, "path");
for (File f : yourDir.listFiles()) {
    if (f.isFile())
        Stringname= f.getName();
        // Do your stuff
}

Have a look at Environment page for more info.

Solution 2:

Try below code

FilesdCard= Environment.getExternalStorageDirectory();
Filedir=newFile(sdCard, "yourpath");
for (File f : dir.listFiles()) {
    if (f.isFile())
        Stringname= f.getName();
        // do whatever you want with filename
}

Solution 3:

File filePath= newFile(File Address);
File[] fileList = filePath.listFiles();
Stringname= fileList [0].getName().toString();

Solution 4:

could you process the string in reverse order(right to left), finding the first slash, then cutting the string at that point and taking the rightmost part of the string as the filename ?

Solution 5:

use method getName() of file object:

file.getName();

Post a Comment for "Get Filenames From A Directory In Android"