0

As part of my app i need to have file browser that lists only specific folders. For eg list only those folders in the path that an image file in any of its subfolders in any level.

I could do it using recursion. But it affects performance. Especially finding folders at top level need many recursive calls. Is there a better way to do it

Please see my code below

public List<GeneralFileItem> getGridItemsList(String root) {
    List<GeneralFileItem> gridItemsList = new ArrayList<GeneralFileItem>();
    File file;
    file = new File(root);
    File list[] = file.listFiles();
    if (list.length == 0) {
        return null;
    }
    for (int i = 0; i < list.length; i++) {
        GeneralFileItem item = new GeneralFileItem();
        File temp_file = new File(file.getAbsolutePath(), list[i].getName());
        if (hasPhoto(temp_file)) {
            item.setPath(file.getAbsolutePath() + "/" + list[i].getName());
            item.setName(list[i].getName());
            if (temp_file.listFiles() != null) {
                item.setType(GeneralFileItem.DIRECTORY_TYPE);
            } else {
                item.setType(GeneralFileItem.FILE_TYPE);
            }
            gridItemsList.add(item);
            //Log.i(TAG,"added"+list[i].getName());
        }
    }
    return gridItemsList;

}

private boolean hasPhoto(File temp_file) {
    //Log.i(TAG,temp_file.getName());

    if (temp_file.listFiles() == null) {
        if (temp_file.getName().toUpperCase().endsWith(("JPG"))) {
            //Log.i(TAG,temp_file.getName()+ "is a photo");
            return true;
        } else
            return false;

    }
    else{
        File list[] = temp_file.listFiles();

        for (int i = 0; i < list.length; i++) {
            if(hasPhoto(list[i]))
                return true;
        }

    }

    return false;
}

I want to know if there is any other way than recursive search. How are the files maintained in Android. I know that various file systems are supported and it varies from hardware manufaturer . I would like to know if there is a way like if there is FileTable from which i can get all .jpg files .

png
  • 4,368
  • 7
  • 69
  • 118
  • try using http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FilenameFilter.html.... – Its not blank May 15 '12 at 12:28
  • thanks for your reply. The filenamefilter interface that you have suggested is useful in getting all image files in current directory. My requirement is to get all directories which has atleast one image in any of its sub folders at any level. – png May 16 '12 at 05:32
  • check this link, hope it will be helpfull http://stackoverflow.com/questions/4195660/get-list-of-photo-galleries-on-android – farhad.kargaran Sep 29 '14 at 07:18
  • @preetha can u pls tell mw how u resolved your issue ???i m also facing same – Erum Dec 16 '14 at 05:09

3 Answers3

0

use FilenameFilter to search for specirfic type of files.

presently you are checking only current directory.However if you wish to search recursively then you will have to check if each file found is a directory or not

Your algo would be something like

File file;
file = new File(root);
File list[] = file.listFiles();
if (list.length == 0) {
    return null;
}
for (int i = 0; i < list.length; i++) {
     if(list[i].isDirectory()){
        //Then go inside it to search for specific file 
        File subList[] = list[i].listFiles();
        //search for jpg..
        }else if("is this jpg"){
         //Add to list
        }
     }

To simplify this you could segregate these into methods as per your requirement.

Its not blank
  • 3,055
  • 22
  • 37
  • i am currently doing recursion. My issue is that its taking huge time to finish recursively searching. I am not able to understand how filefilter will help to improve the performance because here the critical point is recursively parsing . If i am in a folder that has less subfolders, my algo is faster – png May 16 '12 at 13:44
  • the problem is you are first searching througout,adding it to list and then rendering it in gridview.you can try lazy loading see here fr example https://github.com/desertjim/LazyLoadingGridView – Its not blank May 17 '12 at 02:59
0

Here's something I came up! It works like a charm!

First you must create a File, this one is root of SD card

File = rootPrimary = new File(Environment.getExternalStorageDirectory());

than you must list all the files in to a File []

File[] fileArray = rootDirectory.listFiles();

Then you create a ArrayList and set it equal to a method i've created and pass the File[] in.

ArrayList<File> alFolders = new ArrayList<File>();
        alFolder = putImgFldr(fileArray);

This method will add all dorectoryes that contain images with .jpg and .png extension.

private ArrayList<File> putImgFldr(File[] fileArray) {
        // TODO Auto-generated method stub
        ArrayList<File> a = new ArrayList<File>();

        for (int i = 0; i < fileArray.length; i++) {
            if (fileArray[i].isDirectory()) {
                //if file is folder pass that file trough this method
                a.addAll(putImgFldr(fileArray[i].listFiles()));
            } else if (fileArray[i].getAbsolutePath().endsWith(".jpg") || fileArray[i].getAbsolutePath().endsWith(".png")) {
                // if file is img ad it's parent, folder to ArrayList 
                a.add(fileArray[i].getParentFile());
                // this finishes the file searching 
                // because the image has allready bean found
                i = fileArray.length + 1;
            }
        }
        return a;
    }

now You can add another File [] to alFolders if nessesery

alFolders.addAll(putImgFldr(secFileArray));
Crni03
  • 61
  • 6
0

One more example of listing files and directories using Java 8 filter. For instance, here I am using just jpeg images

public static void main(String[] args) {

System.out.println("Files!!");
        try {
            Files.walk(Paths.get("."))
                    .filter(Files::isRegularFile)
                    .filter(c ->
                            c.getFileName().toString().substring(c.getFileName().toString().length()-4).contains(".jpg")
                            ||
                            c.getFileName().toString().substring(c.getFileName().toString().length()-5).contains(".jpeg")
                    )
                    .forEach(System.out::println);

        } catch (IOException e) {
        System.out.println("No jpeg or jpg files");
        }

        System.out.println("\nDirectories!!\n");
        try {
            Files.walk(Paths.get("."))
                    .filter(Files::isDirectory)
                    .forEach(System.out::println);

        } catch (IOException e) {
            System.out.println("No Jpeg files");
        }
}
Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64