如何从 java 的 android 的可移动存储(SD 卡)中获取所有 pdf 文件的链接?

How to get links to all pdf files from removable storage(SD Card) of android in java?

我无法从可移动存储(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;
    }

I have successfully fetched all the links to pdfs in external SD card using the following method

该代码适用于 external storage, not removable storage

I guess I should be able to use the same method to fetch pdfs from internal storage as well

否,因为您无权访问 internal storage,除了专门为您的应用程序预留的目录(例如 Context 上的 getFilesDir())。