RandomyzeEverything RandomyzeEverything - 11 months ago 68
Java Question

Activity getting stuck after trying to get all files with same extension

I'm trying to get all files on the device with same extension using:

private void scanForEpubs() {
File[] file = Environment.getExternalStorageDirectory().listFiles();
for (File ff : file) {
if (ff.isDirectory()) scanForEpubs(f);
if (ff.isFile() && ff.getName().endsWith(".epub")) {
Log.d(TAG, "scanForEpubs: " + ff.toString());
}
}
}


But that activity gets stuck on a blank screen and doesn't load anything.

Stacktrace gets flooded with just one message:

I/art: Background sticky concurrent mark sweep GC freed 49529(2MB) AllocSpace objects, 0(0B) LOS objects, 9% free, 21MB/23MB, paused 10.291ms total 62.505ms

Answer Source

Try changing your code a bit :

 if (ff.isDirectory()) {
    //here instead of f you should put ff
     scanForEpubs(ff);
 }else{
      if (ff.isFile() && ff.getName().endsWith(".epub")) {
            Log.d(TAG, "scanForEpubs: " + ff.toString());
      }
 }

What you were doing was getting into recursive infinite loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download