java – How to get the contents of a folder in android


There is such a question … Suppose I have such a path from the system root /sdcard/downloads/ How can I find out if there are any other folders or files in this folder or is it empty ..?


Use the following example to list the files in a directory:

String path = Environment.getExternalStorageDirectory().toString()+"/folder";
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());

If files == null then the directory is empty

and don't forget permission to read external storage

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Scroll to Top