2017-02-27 74 views
0

我试图显示在我的cordova应用中存储在android设备上的文件列表,但我正在使用cordova -file插件。但是,我在我的应用程序中选择浏览按钮时没有看到该文件,但我在系统“我的文件”Android应用程序中看到该文件。没有看到使用cordova -file插件存储在Android设备上的文件

这里是文件夹,我遍历通

[cordova.file.externalRootDirectory,cordova.file.dataDirectory] 

在Android手机列表中,我看到了文件BU执行以下操作:

  1. 选择/设置/存储/内部存储
  2. 选择探索
  3. 在emnu标题说MYFILES>设备存储
  4. 选择数据或下载..

    在设备 cordova.file.externalRootDirectory决心文件:///存储/模拟/ 0 /下载

不过,我没有看到任何文件

这里我的代码

$scope.showLocalFileOnAndroid = function() { 
      $scope.showLocalAndroidFiles = true; 
      var localURLs = [cordova.file.externalRootDirectory,cordova.file.dataDirectory 
          ]; 
      var index = 0; 
      var i; 
      var errorStr = ''; 
      var fileList = []; 
      var addFileEntry = function (entry) { 
       var dirReader = entry.createReader(); 
       dirReader.readEntries(
        function (entries) { 

         var i; 
         for (i = 0; i < entries.length; i++) { 
          if (entries[i].isDirectory === true) { 
           // Recursive -- call back into this subdirectory 
           addFileEntry(entries[i]); 
          } else { 
           var ext = entries[i].name.split('.').pop(); 
           if (ext === 'doc' || ext === 'docx' || 
            ext === 'rdf' || ext === 'pdf' || ext === 'txt' || 
            ext === 'odt') { 

            fileList.push(entries[i]); // << replace with something useful 
           } 
           index++; 
          } 
         } 
        }, 
        function (error) { 
         console.log('readEntries error: ' + error.code); 
         errorStr += '<p>readEntries error: ' + error.code + '</p>'; 
        } 
       ); 
      }; 
      var addError = function (error) { 
       console.log('getDirectory error: ' + error.code); 
       errorStr += '<p>getDirectory error: ' + error.code + ', ' + error.message + '</p>'; 
      }; 
      for (i = 0; i < localURLs.length; i++) { 
       if (localURLs[i] === null || localURLs[i].length === 0) { 
        continue; // skip blank/non-existent paths for this platform 
       } 
       window.resolveLocalFileSystemURL(localURLs[i], addFileEntry, addError); 
      } 
      $scope.fileList = fileList; 
      $scope.localFileError = errorStr; 
     }; 
+0

可能的重复的未答复的问题在这里:http://stackoverflow.com/questions/40557762/list-the-files-that-are-stored-in-the-download-folder-on-an-android-device – Matthias

+0

谢谢..我看到了答案,并且我复制了一些代码,但是它不起作用。设备上的文件存储在“Internal Storage \ root \ sdcard \ Download”中,并且没有在该代码中定义的路径请参考此路径 – user2570135

+0

这似乎是一个常见问题,[另一个未解答的问题](http://stackoverflow.com/questions/41152137/cordova-file-download-to-android-download-folder)有关科尔多瓦和下载文件夹。 – Matthias

回答

0

Here's东西。也许你必须做这样的事情。这可能不像使用插件的设备那样灵活。

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail); 

var localURLs = [cordova.file.externalRootDirectory, 
       cordova.file.dataDirectory, 
       "file:///Download"]; // or "file:///sdcard/Download" or "file:///storage/Download" or "file:///storage/download" or something 

我用this作为参考。

也许你所缺少的是致电requestFileSystem

+0

谢谢,但它不适用于我..这就是我正在尝试的要做,我有一个html标记输入类型文件,我试图列出设备上的文件,,,在某些版本的android我看到一个系统filechooser,但我不知道如何获得选定的文件...所以,我决定阅读Android文件夹..现在我不知道如何进行 – user2570135

+0

我降级到Android上的API 22,现在我能够读取文件...不知道发生了什么.. – user2570135

相关问题