Android的递归列出SD卡上的文件 [英] android recursively list the files in sd card
问题描述
我要寻找我的sd卡的特定文件。所以,我试图列出完整的文件和搜索模式。请参考下面
I want to search for a particular file in my sd card . So i am trying to list complete files and search for the pattern. Please see code below
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File(root_sd);
listfiles( new File(root_sd));
}
public void listfiles(File file) {
//file = new File(path);
// file = new File( root_sd ) ;
File list[] = file.listFiles();
Log.i("DIR", "PATH" +file.getPath());
for (int i = 0; i < list.length; i++) {
// myList.add( list[i].getName() );
File temp_file = new File(file.getAbsolutePath(),list[i].getName());
Log.i("DIR", "PATH" +temp_file.getAbsolutePath());
if (temp_file.isFile() && temp_file.listFiles() != null) {
Log.i("inside", "call fn");
listfiles(temp_file);
} else {
if (list[i].getName().toLowerCase().contains("pattern1"))
Log.i("File", i + list[i].getName());
if (list[i].getName().toLowerCase().contains("pattern2"))
Log.i("File", i + list[i].getName());
}
}
搜索这里只一级正在发生的事情。这种情况如果(temp_file.isFile()及和放大器;!temp_file.listFiles()= NULL)
总是返回false,因此递归调用没有发生。
请帮我解决它。感谢您的回答和时间。
is always returning false and thus recursive call not happening. Please help me to fix it. Thanks for your answer and time.
推荐答案
如果在 temp_file.isFile()
返回true,那么就意味着它是一个文件,因此 temp_file.listFiles()
自动变为零从而使你的整个语句错误的。因此,合并后的语句总是返回false。你需要的是一个 ||
在&放大器;&安培;
if the temp_file.isFile()
returns true then it means it is a file and hence temp_file.listFiles()
automatically becomes null thus making your entire statement false. Thus your combined statement returns a false always. What you need is a ||
in place of &&
.
这篇关于Android的递归列出SD卡上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!