Find all files on sd card

后端 未结 1 835
无人共我
无人共我 2021-01-20 00:46

I want to find all files stored on sdCard. I used this code:

List dir = new ArrayList();
List files = new ArrayList

        
1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-20 01:00

    You can't add to a list while you're iterating over it. Try this instead:

    List dir = new ArrayList();
    List files = new ArrayList();
    
       File root = new File (Environment.getExternalStorageDirectory().getAbsolutePath());
    
        List rootDirs = //... code to find root directories
    
        for (File f : rootDirs) { // find in root...
           scan(f);
        }
    
        Log.i("files", "" + files.size());
        Log.i("dir", "" + dir.size());
    
    
    }
    
     public void scan (File path) {
    
        for (File f : path.listFiles()) {
            if (f.isFile()) {
                files.add(f);
            }
            else {
                dir.add(f);
                scan(f);
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题