[英]Get all files in all folders in internal and external storage
I'm working on a project on android studio and I need help.. How can I search for a file by its name in all folders in internal and external storage?? 我正在android studio上的一个项目上工作,需要帮助。如何在内部和外部存储中的所有文件夹中按文件名搜索文件?
I tried using for loop to search in internal.. And I tested if the file doesn't exist , the path will be the old path+the folder name. 我尝试使用for循环在内部进行搜索。并且测试了文件是否不存在,该路径将为旧路径+文件夹名称。 It didn't work out 它没有解决
public void getAllDir(File dir) {
String pdfPattern = ".pdf or .txt or .jpg etc..";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
getAllDir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(pdfPattern)){
//Do what ever u want
}
}
}
}
}
and call this function like
getAllDir(Environment.getExternalStorageDirectory());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.