如何在Java中从Android可移动存储(SD卡)获取所有PDF文件的链接?

huangapple go评论69阅读模式
英文:

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(), "搜索结束:" + String.valueOf(pdfCount), 0).show();
                break;
              }
          }
        }

      return pdfs;
    }
英文:

I am unable to fetch pdf files from removable storage(SD Card).

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

    public String findBooks() {

        String pdf = &quot;.pdf&quot;;
        String epub = &quot;.epub&quot;;

        File dir = Environment.getExternalStorageDirectory();
        Queue&lt;File&gt; toVisit = new PriorityQueue&lt;&gt;(); 
        toVisit.add(dir);
        String pdfs = &quot;&quot;;
        int pdfCount = 0;

        if (toVisit.size() != 0) {
          while(true){
              File file = toVisit.remove();
              File[] tmpList = file.listFiles();
              int i;
              for(i = 0; i &lt; 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() + &quot;,&quot;;
                    pdfCount += 1;
                }
              }

              if(toVisit.size() == 0){
                Toast.makeText(getReactApplicationContext(), &quot;End of search: &quot;+ String.valueOf(pdfCount), 0).show();
                break;
              }
          }
        }

      return pdfs;
    }

答案1

得分: 1

我已经成功使用以下方法获取了外部SD卡中所有PDF链接。

那段代码适用于外部存储,而不是可移除存储

我猜我应该能够使用相同的方法从内部存储中获取PDF。

不可以,因为您无法访问内部存储,除了专门为您的应用程序设置的目录之外(例如,Context上的getFilesDir())。

英文:

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

That code is working with external storage, not removable storage.

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

No, because you have no access to internal storage, other than the directories set aside specifically for your app (e.g., getFilesDir() on Context).

huangapple
  • 本文由 发表于 2020年10月1日 19:04:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64154065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定