27

I am using Cordova 4.2.0 with plugins File and media. I have some mp3 files in audio folder inside www directory. I am able to play files through media plugin using path cordova.file.applicationDirectory+"www/audio/01.mp3" and trying to get all available files list using window.resolveLocalFileSystemURL but unable to do so. Please see below code always goes to fail function.

window.resolveLocalFileSystemURL(cordova.file.applicationDirectory+'/www/audio', 
    function(dirEntry){
        alert('in');
    }, 
    fail
);

Please guide me the best way.

Raviranjan Mishra
  • 849
  • 2
  • 11
  • 26

1 Answers1

62

You can use this function to get all available files in www/audio/ folder

function listDir(path){
  window.resolveLocalFileSystemURL(path,
    function (fileSystem) {
      var reader = fileSystem.createReader();
      reader.readEntries(
        function (entries) {
          console.log(entries);
        },
        function (err) {
          console.log(err);
        }
      );
    }, function (err) {
      console.log(err);
    }
  );
}
//example: list of www/audio/ folder in cordova/ionic app.
listDir(cordova.file.applicationDirectory + "www/audio/");
MrP
  • 1,408
  • 18
  • 23
  • 2
    why are these functions not available in documentation? – HalfWebDev May 12 '17 at 09:56
  • 1
    This function use just html5 features, except for "cordova.file.applicationDirectory" (Documentation is here: https://www.w3.org/TR/2012/WD-file-system-api-20120417/) – MrP May 15 '17 at 11:00
  • Great ! I was looking at cordova plugin API for file transfer. – HalfWebDev May 16 '17 at 06:20
  • The docs is here: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/ – Vadim Nov 07 '17 at 10:34
  • I get 0 results when listing `file:///storage/emulated/0/DCIM/Camera/` does this have to do with Android permissions? – A.W. Apr 26 '20 at 17:30
  • Yes, look around android persmission. I don't know if you have permission to read this volume. – MrP Apr 27 '20 at 10:30
  • Please see this issue if you are having trouble. https://github.com/apache/cordova-plugin-file/issues/426 – tomasantunes Sep 24 '22 at 11:10