public void check4PDF(File dir) {
String pdfPattern = ".pdf";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
check4PDF(listFile[i]);
} else {
if (listFile[i].getName().endsWith(pdfPattern)) {
FileDetails mFileDetails = new FileDetails();
mFileDetails.setmFilename(listFile[i].getName());
mFileDetails.setmPath(listFile[i].getAbsolutePath());
pdfDetails.add(mFileDetails);
}
}
}
}
}
此代码不会在nexus中给出任何结果。
if (!Environment.MEDIA_MOUNTED.equals(storageState)
&& !Environment.MEDIA_MOUNTED_READ_ONLY.equals(storageState)) {
Toast.makeText(getActivity(), "SD Card not found.",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(),
Environment.getExternalStorageDirectory() + "",
Toast.LENGTH_SHORT).show();
ScanSDCardTask temp = new ScanSDCardTask(
Environment.getExternalStorageDirectory(), getActivity());
temp.execute(".pdf");
}
我认为路径上的问题请帮助。
答案 0 :(得分:1)
以这种方式搜索..比搜索文件更快更有效..
private void checkForPdf(File file) {
try {
if (file.exists()) {
File[] listFiles = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return filename.toLowerCase().endsWith(".pdf");
}
});
for (File file2 : listFiles) {
//here you will get all pdf files..
}
}
} catch (Exception e) {
}
}