Chad White
Chad White

Reputation: 309

Android How can I read filenames from a specific folder?

I would like to scan a specific folders for all filenames inside to save them to an array.

But how can I access the sd Card to do this?

Help would be great (io / enviroment???)

Upvotes: 0

Views: 209

Answers (2)

Salman Khakwani
Salman Khakwani

Reputation: 6714

You can use the following code:

String filesDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()+"/YOUR_SPECIFIC_FOLDER/"
File fileList = new FilefilesDirectory 
if (fileList != null)
{
File[] filenames = fileList.listFiles();
//Loop through each file and get the file name
String fileNamesArray[] = new String[filenames.length]; 
int i=0;
for (File tempFile : filenames)
    {
        fileNamesArray[i] = tempFile.getName();
        i++;
    }
}

Upvotes: 0

Nick
Nick

Reputation: 1852

Environment.getExternalStorageDirectory() will link you to the SD card if one is inserted in the phone. Otherwise it just links you to your phone storage. Here's a link to more information on Environment http://developer.android.com/reference/android/os/Environment.html.

File file = new File(Environment.getExternalStorageDirectory()+"/folder/");
File[] files = file.listFiles();
String[] fileNames = new String[files.length];
for(int i = 0; i < files.length; i++) 
    fileNames[i] = files[i].getName();

Upvotes: 1

Related Questions