path = Environment.getExternalStorageDirectory().toString()
public static ArrayList<String> listFoldersAndFilesFromSDCard(String path) {
ArrayList<String> arrayListFolders = new ArrayList<String>();
try {
File[] dir = new File(path).listFiles();
// Here 'dir' will give you a list of folder/files in the hierarchy
if (null != dir && dir.length > 0) {
for (int i = 0; i < dir.length; i++) {
if (dir[i].isDirectory()) {
arrayListFolders.add(new File(dir[i].toString()).getName());
// Here you can call recursively this
// function for file/folder hierarchy
}
else {
// Do whatever you want with the files
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return arrayListFolders;
}
1条答案
按热度按时间0dxa2lsx1#
你可以这样做。
我将采取 * 路径 * 是设备存储。
如果您使用的是Android Studio或类似的工具,请将调试点放在:
您将看到文件/文件夹层次结构。