2014-01-24 31 views
2

我的應用程序我使用phonegap構建爲android編譯它。我需要創建一個持久的文件夾來存儲一些音頻文件,但我不能使它工作...這裏是我的代碼:config.xml中無法獲取getDirectory在Android設備上使用phonegap

<gap:plugin name="org.apache.cordova.file" version="0.2.4" /> 

在我的index.html:

document.addEventListener("deviceready", onDeviceReady, false); 

// device APIs are available 
// 

function onDeviceReady() { 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) { 
     fileSystem.root.getDirectory("mynewfolder", {create: true}, gotDir); 
     console.log(fileSystem.root); 

} 

function gotDir(dirEntry) { 
     dirEntry.getFile("myfile.txt", {create: true, exclusive: true}, gotFile); 
} 

function gotFile(fileEntry) { 
     // Do something with fileEntry here 
} 

我沒有解釋你有一些線索嗎?控制檯什麼都不給。

回答

3

嗯,你的代碼是好的,但問題是你錯過了錯誤回調....所以,你的修改後的代碼看起來像

document.addEventListener("deviceready", onDeviceReady, false); 

// device APIs are available 
// 

function onDeviceReady() { 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) { 
     fileSystem.root.getDirectory("mynewfolder", {create: true}, gotDir,fail); 
     console.log(fileSystem.root); 

} 

function gotDir(dirEntry) { 
     dirEntry.getFile("myfile.txt", {create: true, exclusive: true}, gotFile,fail); 
} 

function gotFile(fileEntry) { 
     // Do something with fileEntry here 
} 

function fail(error) { 
    console.log(error.code); 
} 
+1

感謝您的幫助。事實上,它的失效功能是有效的! –

相關問題