我在sd卡上有一个XML文件。我要搜索整个sd卡,找到那个文件。之后,我必须解析该文件。
我做了一个类似下面。
static final String PATH = Environment.getExternalStorageDirectory()
.getPath() + "/download/";
static final String FILE_NAME = "aura_key.xml";
static File isKeyExist() {
File file = new File(PATH+FILE_NAME);
if(file.exists())
{
return file;
}else
return file;
}
如果你想在整个外部存储中搜索,你需要查找所有目录和子目录等…
这里有一个示例解决方案,只需调用第一个方法与您的文件名(在您的情况下包括文件扩展名: )searchForFileInExternalStorage("aura_key.xml");
和下面是方法:
public File searchForFileInExternalStorage(String filename) {
File storage = Environment.getExternalStorageDirectory();
return searchForFileInFolder(filename, storage);
}
public File searchForFileInFolder(String filename, File folder) {
File[] children = folder.listFiles();
File result;
for (File child : children) {
if (child.isDirectory()) {
result = searchForFileInFolder(filename, child);
if (result != null) {
return result;
}
} else {
// replace equals by equalsIgnoreCase if you want to ignore the
// case of the file name
if (child.getName().equals(filename)) {
return child;
}
}
}
return null;
}
public static boolean checkdirAvail(String yourpath) {
boolean ret = true;
File file = new File(Environment.getExternalStorageDirectory(), path);
if (!file.exists()) {
if (!file.mkdirs()) {
Log.e("TravellerLog :: ", "Problem creating Image folder");
ret = false;
}
}
return ret;
}