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 any way to this?
I have worked on a basic one but still haven't get the desired result. It doesn't work. Here is my code:
File[] files_array;
files_array = new File(Environment.getExternalStorageDirectory().getAbsolutePath()).listFiles();
Please help. Thank you.
Edit:
This method returns 0 size. I don’t know 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;
}
}