标题翻译
Search All Files and Folders On Phone?
问题
我想要搜索所有文件和文件夹,以查找手机或平板电脑上的文档。似乎我只能搜索特定的目录,但我想要搜索所有pdf和docx文档的文件。类似于:
getLocal = (Button)findViewById(R.id.localFileButton);
getLocal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
findFiles(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS));
}
});
private void findFiles(File dir){
//We could put these in an enumeration
String pdfPattern = ".pdf";
String docPattern = ".docx";
String txtPattern = ".txt";
File listFile[] = dir.listFiles();
if (listFile != null){
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
findFiles(listFile[i]);
} else {
if (listFile[i].getName().endsWith(txtPattern)){
//Do what ever u want
}
}
}
}else{
Toast.makeText(this, "No files on the phone", Toast.LENGTH_SHORT).show();
}
}
我不想将搜索限制在一个文件夹中,我想要搜索任何可用的文件夹,有办法做到这一点吗?
英文翻译
I'm looking to search all files and folders for documents that are on a phone or tablet. Seems like I can only search certain directories, but want to search for all files for pdfs and docx documents. Something like:
getLocal = (Button)findViewById(R.id.localFileButton);
getLocal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
findFiles(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS));
}
});
}
private void findFiles(File dir){
//We could put these in an enumeration
String pdfPattern = ".pdf";
String docPattern = ".docx";
String txtPattern = ".txt";
File listFile[] = dir.listFiles();
if (listFile != null){
for (int i = 0; i <= listFile.length; i++) {
if (listFile[i].isDirectory()) {
findFiles(listFile[i]);
} else {
if (listFile[i].getName().endsWith(txtPattern)){
//Do what ever u want
}
}
}
}else{
Toast.makeText(this, "No files on the phone", Toast.LENGTH_SHORT).show();
}
}
}
I don't want to limit the search to just one folder, I want to search for any available folders, is there a way to do this?
专注分享java语言的经验与见解,让所有开发者获益!
评论