0

In my little file explorer app I show the number of sub-elements in each directory like:

enter image description here

To set those numbers, I launch an AsyncTask (so the ListView won't get "laggy") from my Adapter's getView() method and that for each item in the list. However, when viewing some system directories (like "/" for example) with huge number of subdirs and files, the garbage collector is going insane and performance drops significantly (multiple instances of my AsyncTask still stay in memory even after the app gets finished).

I'm quite sure this is related to how I implemented the subdirs and subfiles check that I'm doing inside the AsyncTask, but the following recursive approach is the only thing I could think of:

   //countHidden is a boolean indicating whether to count hidden files
   private int[] getSubFilesCount(File root) {
    int fcount = 0;
    int dcount = 0;
    File[] files = root.listFiles();
    if (files != null)
        for (File f : files) {
            if (f.isDirectory()) {
                getSubFilesCount(f);
                if (f.isHidden()) {
                    if (countHidden)
                        dcount++;
                } else {
                    dcount++;
                }
            } else {
                if (f.isHidden()) {
                    if (countHidden)
                        fcount++;
                } else {
                    fcount++;
                }
            }
        }
    int[] tcount = { fcount, dcount };
    return tcount;
}

The question: is there any alternative to get the number of subdirectories and files that will work faster then the approach posted above?

Droidman
  • 11,485
  • 17
  • 93
  • 141

1 Answers1

0

You could do what's suggested here which is effectively the same as you're doing but slightly more succinct (if you don't mind me saying so). Obviously you'd have to do it for files and directories. I believe Java 7 provides a better way of doing this but unfortunately you're limited to Java 6 at the moment with Android.

Community
  • 1
  • 1
D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97