尝试获取具有相同扩展名的所有文件后,活动卡住

时间:2017-12-23 07:14:46

标签: java android

我正在尝试使用相同的扩展程序获取设备上的所有文件:

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());
        }
    }
}

但是该活动卡在空白屏幕上并且没有加载任何内容。

Stacktrace只会被一条消息淹没:

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

2 个答案:

答案 0 :(得分:2)

尝试更改一下代码:

 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());
      }
 }

你正在做的是进入递归无限循环。

答案 1 :(得分:1)

{% trans username %}