Find all files on sd card

后端 未结 1 834
无人共我
无人共我 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<File> dir = new ArrayList<File>();
    List<File> files = new ArrayList<File>();
    
       File root = new File (Environment.getExternalStorageDirectory().getAbsolutePath());
    
        List<File> 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)
提交回复
热议问题