2015-12-20 93 views
2

我正在開發一個音樂應用程序,我需要從android設備中檢索所有的mp3文件。我怎樣才能做到這一點與離子和科爾多瓦?離子科爾多瓦從SD卡獲取所有的mp3文件

我已經嘗試過使用cordova文件插件,但我甚至找不到列出目錄中所有文件的方法。請幫忙。

window.resolveLocalFileSystemURI(cordova.file.externalRootDirectory, function(fileSystem) { 
    var directoryReader = fileSystem.createReader(); 
    directoryReader.readEntries(function(entries) { 
    //alert(JSON.stringify(directoryReader)); 
    for(i=0;i<entries.length;i++) 
    { 
     alert(JSON.stringify(entries[i].name)); 

    } 
    //alert(entries); 
    //console.log(entries); 
    }); 
}); 

上面的代碼只是給出了SD卡中的目錄名稱。我需要一個邏輯來查找所有目錄和子目錄中的mp3文件。

回答

0

此例程將列出所選文件系統中的所有文件和文件夾。

import { File } from "@ionic-native/file"; 
    import { Diagnostic } from "@ionic-native/diagnostic"; 

    constructor(
     ... 
     public file: File, 
     public diagnostic: Diagnostic 
     ){ 


    // ------------------------- 
    listFileSys(which){ 

    // <which> chooses the file system - see switch below 
    this.jcDebug += "\n" + "listFileSysSKOFLO(" + which + ")"; 

    let fileSysArray = []; 
    let basePath = ""; 

    let tag = "unknown"; 

    // ************** RECURSE ************* 
    let jcListDir = (thisDir)=>{ 

     tag = "jcListDir: [" + thisDir + "]"; 

     this.file.listDir(basePath, thisDir) 
     .then((data)=>{ 
     tag = "listDir:" + thisDir; 
     this.jcError += "\n" + tag + ":" + JSON.stringify(data); 
     for(let ii = 0; ii < data.length; ii += 1){ 
      this.jcError += "\n" + data[ii].name + (data[ii].isDirectory? " [D]" : " [F]"); 

      let currentPath = thisDir; 
      currentPath += (currentPath.length ? "/" : ""); 
      currentPath += data[ii].name; 
      this.jcError += "\n" + "currentPath:" + currentPath; 

      fileSysArray.push(currentPath); 

      if(data[ii].isDirectory && ok2recurse){ 
      jcListDir(currentPath);   // RECURSE !!! 
      } 
     } 
     }, (errData)=>{ 
     tag = "listDir"; 
     this.jcError += "\n" + tag + ":ERR:" + JSON.stringify(errData); 
     }); 
    }; 
    // ************** RECURSE ************* 

    // *********************** 
    let runListDir =()=>{ 
     this.jcDebug += "\n" + "basePath:" + basePath; 

     // !!! START listing from basePath !!! 
     jcListDir("."); 
    } 

    // *********************** 
    switch(which) 
    { 
     case 1: 
     this.diagnostic.getExternalSdCardDetails() 
     .then((data) => { 
      this.jcDebug += "\n" + "sd:" + JSON.stringify(data); 
      this.jcDebug += "\n" + "Number of cards: " + data.length; 
      for(let ii = 0; ii < data.length; ii += 1){ 
      let thisElem = data[ii]; 
      if(thisElem.type.toLowerCase() === "application" && thisElem.canWrite){ 
       basePath = thisElem.filePath; 
       break; 
      } 
      } 
      if(!basePath){ 
      this.jcDebug += "\n" + "no SD card found"; 
      return; 
      } 
      runListDir(); 
     }, (errData)=>{ 
      tag = "getExternalSdCardDetails"; 
      this.jcError += "\n" + tag + ":ERR:" + JSON.stringify(errData); 
     }); 
     break; 
     case 2: 
     basePath = this.file.externalDataDirectory; 
     this.jcError += "\n" + "externalDataDirectory:" + basePath; 
     runListDir(); 
     break; 
     case 3: 
     basePath = this.file.dataDirectory; 
     this.jcError += "\n" + "dataDirectory:"; 
     runListDir(); 
     break; 
     default: 
     alert("which???:" + which); 
     return; 
    } 

    // wait for all to comnplete, then show 
    // assume 1000 ms is adequate delay per promise 
    let lastFileSysLen = -1 
    let checkCount = 30; // max 30 * 1000 ms = 30 seconds 

    // ************** RECURSE ************* 
     let checkComplete =() => { 
     this.jcDebug += "\n" + "checkComplete " + checkCount + " [" + fileSysArray.length + "]"; 
     setTimeout(()=>{ 

     // fileSysArr length stable? 
     if(fileSysArray.length === lastFileSysLen){ 
      checkCount = 0; 
     } 
     lastFileSysLen = fileSysArray.length; 

     checkCount -= 1; 
     if(checkCount > 0){ 
      checkComplete(); // recurse 
     } else { 

      // STOP !!! and show FileSysArray 
      this.jcInfo += "\n" + "show FileSysArray"; 
      this.jcInfo += "\n" + "fileSysArray.length = " + " [" + fileSysArray.length + "]"; 

      fileSysArray.sort(); 

      for(let elem of fileSysArray){ 
      this.jcInfo += "\n" + elem; 
      } 
     } 
     }, 1000); 
    }; 
    // ************** RECURSE ************* 
    checkComplete(); 
    } 

必須有搞清楚時承諾的所有解決的一個更好的辦法...

相關問題