Search code examples
androidarraysfileandroid-file

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


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.


Solution

  • 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();