我试图从SD卡搜索文件,所以我可以删除多个和重复的文件.`
private List<String> searchForFileNameContainingSubstring(String substring)
{
path = Environment.getExternalStorageDirectory().getPath() + "/";
//This is assuming you pass in the substring from input.
File file = new File(path); //Change this to the directory you want to search in.
List<String> filesContainingSubstring = new ArrayList<String>();
if (file.exists() && file.isDirectory())
{
String[] files = file.list(); //get the files in String format.
for (String fileName : files)
{
if (fileName.contains(substring))
filesContainingSubstring.add(fileName);
}
}
for (String fileName : filesContainingSubstring)
{
System.out.println(fileName); //or do other operation
}
return filesContainingSubstring; //return the list of filenames containing substring.
}
如何从sdcard /目录中扫描其他子文件夹
它仅显示sdcard目录的结果
答案 0 :(得分:0)
您可以使用 Apache Common FileUtils.listFiles方法。 您可以通过将第三个参数设置为true来递归搜索整个文件夹。 此外,您可以通过将第二个参数传递给String数组来定位特定的文件扩展名,如下所示。如果要将任何扩展名作为目标,请传递null。
注意: 扩展名称不包括&#39;。&#39;它&#34; jpg&#34;而不是&#34; .jpg&#34;
String[] extensions = {"png","jpg"};
Collection images = FileUtils.listFiles(new File("dirPath"),extensions, true);
for (Object obj : images){
File file = (File) obj;
// your code logic
}