Android: How to list all the files in a directory in to an array?

Android_Noob picture Android_Noob · Jan 17, 2015 · Viewed 33.7k times · Source

I have a variable "directorypath". It has the path to a directory in sd card.

Ex: "sdcard/images/scenes"

Now, I want to list all the files in the "directorypath" and want an Array to store all the file names in it.

Ex: Array[0]= Filename1, Array[1]= Filename2 Etc... 

I tried something like Array[] myArray = directorypath.listfile(); but didn't work..! Can anyone give me the code or at least help me? Much appreciated.

Answer

user4463890 picture user4463890 · Jan 17, 2015

I think the problem you are facing is related to the external storage directory file path. Don't use whole path as variable if you can access with environment.

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

Also, you can use the API listfiles() with file object and it will work. For eg ::

File f = new File(path);        
File file[] = f.listFiles();