[英]How can I read all files and directory in a device?
Is it possible to develop a Java application that can read all files and directories in the device's memory?是否可以开发一个Java应用程序,可以读取设备的memory中的所有文件和目录?
I think it's not possible (for security reasons), but I need another opinion.我认为这是不可能的(出于安全原因),但我需要另一种意见。
You can do it like this.你可以这样做。
I will take path is the device storage.我将采取的路径是设备存储。
If you are using Android Studio or a likewise tool, put the debug point next to:如果您使用的是Android Studio或类似工具,请将调试点放在:
File[] dir = new File(path).listFiles();
And you will see the file/folder hierarchy.您将看到文件/文件夹层次结构。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.