Android How do I check the number of files in a directory

22,366

Solution 1

Check the listFiles method of File that will give you the list of children.

Solution 2

    File file=new File("/mnt/sdcard/yourfolder");
        File[] list = file.listFiles();
        int count = 0;
        for (File f: list){
            String name = f.getName();
            if (name.endsWith(".jpg") || name.endsWith(".mp3") || name.endsWith(".some media extention"))
               count++;
            System.out.println("170 " + count);
        }

Solution 3

try this

File childfile[] = dir .listFiles();
for (File file2 : childfile) {
    Log.v("file name is ", file2.getName());
}

here listFiles() return all the childfiles for the "dir" and iterating this will help you to get the names of the childfiles

Solution 4

Simple:

File dir = new File(path); // "/mnt/sdcard/yourfolder"

long totalNumFiles = dir.listFiles().length;
Share:
22,366
Anshul
Author by

Anshul

Updated on July 27, 2022

Comments

  • Anshul
    Anshul almost 2 years

    I am creating a directory in internal memory of device for storing files for application use.I am using:

    File dir = context.getDir(userfavorites, Context.MODE_PRIVATE);
    

    The above code will create a new directory if it does not exists or returns me the existing file object if directory has already been created.What I want to check is the number and name of files this directory contains.How do I use this file object to accomplish this task.

    NOTE:I don't want to use external memory of device.And I can't avoid creating directory as well because the files in directory has to be separated from other files that are not in directory.

    UPDATED CODE:

    private static boolean IsFoldercontainsFiles(Context context) {
            // TODO Auto-generated method stub
            File dir = context.getDir(USER_FAV, Context.MODE_PRIVATE);
            if (dir.exists()) {
                        if (dir.listFiles() == null){
                         return false;} //it never execute this line even though its null
                        else{
                File childfile[] = dir.listFiles();
                Log.i("file no is ", Integer.toString(childfile.length));
                if (childfile.length == 0) {
                    return false;
                } else {
                    return true;
                }
                    }
            } else {
                return false;
            }
        }
    

    Thanks in advance

  • Anshul
    Anshul over 11 years
    But if i check if (dir.listFiles() == null) then it always say its not null even though thr is nothing in the directory.Am I checking the wrong way for null check?Please help
  • Anshul
    Anshul over 11 years
    But if i check if (dir.listFiles() == null) then it always say its not null even though thr is nothing in the directory.Am I checking the wrong way for null check?Please help
  • G_S
    G_S over 11 years
    once have a look at this.. developer.android.com/reference/java/io/File.html#listFiles(‌​) . I think according to this the method listFiles() returns null only if the particular file is not a directory
  • Demon App Programmer
    Demon App Programmer over 4 years
    Yours should be accepted as you have told how to do it.