Roger Travis Roger Travis - 1 month ago 6
Android Question

How to list files in an android directory?

Here's my code so far:

String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";

AssetManager mgr = getAssets();

try {

String list[] = mgr.list(path);
Log.e("FILES", String.valueOf(list.length));

if (list != null)
for (int i=0; i<list.length; ++i)
{
Log.e("FILE:", path +"/"+ list[i]);
}

} catch (IOException e) {
Log.v("List error:", "can't list" + path);
}


Yet while I do have files in that dir, it returns me list.length = 0... any ideas?

Thanks!

Answer

Try this:

String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
    Log.d("Files", "FileName:" + files[i].getName());
}