无法使用phonegap在Android设备上运行getDirectory

时间:2014-01-24 22:53:07

标签: jquery cordova phonegap-build

我的应用程序我使用phonegap build来为android编译它。我需要创建一个持久性文件夹来存储一些音频文件,但我无法使它工作......这是我的代码:

在config.xml中

<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
}

我没有解释你有一些线索吗?控制台什么都没有。

1 个答案:

答案 0 :(得分:4)

你的代码很好,但问题是你错过了错误回调....所以修改后的代码看起来像

 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);
}
相关问题