【问题标题】:How to get links to all pdf files from removable storage(SD Card) of android in java?如何在java中从android的可移动存储(SD卡)获取所有pdf文件的链接?
【发布时间】: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


【解决方案1】:

我已使用以下方法成功获取外部 SD 卡中 pdf 的所有链接

该代码适用于external storage,而不是removable storage。

我想我应该也可以使用相同的方法从内部存储中获取 pdf 文件

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

【讨论】:

  • @peter 这非常清楚!非常感谢。但是我如何访问可移动存储上的 pdf 文件
  • @CodeNinja:你不知道,尤其是在较新版本的 Android 上。使用 ACTION_OPEN_DOCUMENT 并让用户从用户可用的本地和云存储选项中选择 PDF。
猜你喜欢
  • 2011-06-01
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
  • 1970-01-01
  • 2021-05-23
  • 2020-01-11
  • 1970-01-01
相关资源
最近更新 更多