【发布时间】:2020-10-01 11:04:36
【问题描述】:
我无法从可移动存储(SD 卡)中获取 pdf 文件。
我已经使用以下方法成功获取了外部存储中所有pdf的链接
public String findBooks() {
String pdf = ".pdf";
String epub = ".epub";
File dir = Environment.getExternalStorageDirectory();
Queue<File> toVisit = new PriorityQueue<>();
toVisit.add(dir);
String pdfs = "";
int pdfCount = 0;
if (toVisit.size() != 0) {
while(true){
File file = toVisit.remove();
File[] tmpList = file.listFiles();
int i;
for(i = 0; i < tmpList.length; i++){
if (tmpList[i].isDirectory()) {
toVisit.add(tmpList[i]);
}
else if (tmpList[i].getName().endsWith(pdf) || tmpList[i].getName().endsWith(epub) ){
pdfs += tmpList[i].toString() + ",";
pdfCount += 1;
}
}
if(toVisit.size() == 0){
Toast.makeText(getReactApplicationContext(), "End of search: "+ String.valueOf(pdfCount), 0).show();
break;
}
}
}
return pdfs;
}
【问题讨论】:
-
要获取可移动 micro sd 卡的路径,请查看
getExternalFilesDirs()返回的第二项。您的其余代码可以保持不变。
标签: java android android-sdcard