0

I'm currently trying to scan the file system for mp3 files. I did an AsyncTask that scans a directory and if finds a subfolder, it calls doInBackground() again for that subfolder and so on. However, this seems to crash the application. Is there any way to call doInBackground() recursively?

@Override
    protected List<File> doInBackground(String... params) {

        if (isCancelled()) return foundFiles_;
        publishProgress(String.valueOf(foundFiles_.size()));
        File file = new File(params[0]);
        String [] files = file.list();
        for(String fileName : files)
        {
            File foundFile = new File(fileName);
            if(foundFile.isDirectory())
            {
                doInBackground(fileName);
            }
            else
                foundFiles_.add(foundFile);
        }

        return foundFiles_;
    }
Lucian Radu
  • 105
  • 1
  • 10
  • doInBackground() is just a method running in another thread, so you should be able to recursively calling him easily. Please, post some code to help you. – Daniel Conde Marin Jan 23 '14 at 19:16
  • What error are you getting?, please post also logcat trace – Daniel Conde Marin Jan 23 '14 at 19:21
  • here duplicated thread in same problem take look http://stackoverflow.com/questions/11482204/how-to-recursively-scan-directories-in-android http://stackoverflow.com/questions/2056221/recursively-list-files-in-java http://stackoverflow.com/questions/2534632/list-all-files-from-a-directory-recursively-with-java – mohammed momn Jan 23 '14 at 19:21

1 Answers1

2

Is there any way to call doInBackground() recursively?

I would not do that. Instead, move your "scans a directory and if finds a subfolder" into some other method (e.g., scanDirectory()), which uses recursion. Then, have doInBackground() do the first call to your new method.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491