I looking for help to list all files in Android external storage device. I want to look up in all the folders, including the subfolders for the main folder. Is there a way to this?
I have worked on a basic one, but I still haven't got the desired result. It doesn't work.
Here is my code:
File[] files_array;
files_array = new File(Environment.getExternalStorageDirectory().getAbsolutePath()).listFiles();
This method returns 0 size. What is the matter? This is my activity:
public class Main extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<File> files = getListFiles(Environment.getExternalStorageDirectory());
setListAdapter(new ArrayAdapter<File>(Main.this, android.R.layout.simple_list_item_1, files));
Toast.makeText(this, "" + files.size(), Toast.LENGTH_LONG).show();
}
private List<File> getListFiles(File parentDir) {
// On first call, parentDir is your sdcard root path
ArrayList<File> inFiles = new ArrayList<File>(); // Initialize an array list to store file names
File[] files = parentDir.listFiles(); // List all files in this directory
for (File file : files) {
if (file.isDirectory()) { // If the file is a directory
inFiles.addAll(getListFiles(file)); // *** Call this recursively to get all lower level files ***
}
}
return inFiles;
}
}