Phonegap本地存储 - 文件API

时间:2015-03-31 16:44:10

标签: javascript android cordova local-storage

我的应用无法将文件写入本地存储。我按照API documentation进行了操作。我的代码是:

/*
 * 
 * Device Ready
 * 
 */

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

//Crodova is ready
function onDeviceReady() {
    // Now safe to use device APIs
    //Load the main html on startup
    loadContent("main.html");
    //Prepare to laod the seetings and history file
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, failFS);

}
/*
*
* The File Storage
*
*/
could, know all old GPS positions
function gotFS() {
    fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

function failFS(error) {
    console.log(error.code);
}

function gotFileEntry(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log("contents of file now 'some sample text'");
        writer.truncate(11);  
        writer.onwriteend = function(evt) {
            console.log("contents of file now 'some sample'");
            writer.seek(4);
            writer.write(" different text");
            writer.onwriteend = function(evt){
                console.log("contents of file now 'some different text'");
            }
        };
    };
    writer.write("some sample text");
}

我正在使用android模拟器和logcat我得到以下输出:

[linux@linux ~]$ adb logcat *:S CordovaLog:D
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
D/CordovaLog( 1101): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1101): message: Timeout expired
D/CordovaLog( 1101): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1101): message: Timeout expired
D/CordovaLog( 1101): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1101): message: Timeout expired
D/CordovaLog( 1101): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1101): message: Timeout expired
D/CordovaLog( 1101): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1101): message: Timeout expired
D/CordovaLog( 1101): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1101): message: Timeout expired
D/CordovaLog( 1101): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1101): message: Timeout expired
D/CordovaLog( 1101): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1101): message: Timeout expired
D/CordovaLog( 1101): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1101): message: Timeout expired
I/CordovaLog( 1286): Changing log level to DEBUG(3)
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1169 : deviceready has not fired after 5 seconds.
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1162 : Channel not fired: onPluginsReady
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1162 : Channel not fired: onCordovaReady
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 43 : main.html
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1059 : processMessage failed: Error: ReferenceError: fileSystem is not defined
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1060 : processMessage failed: Stack: ReferenceError: fileSystem is not defined
D/CordovaLog( 1286):     at gotFS (file:///android_asset/www/js/index.js:306:5)
D/CordovaLog( 1286):     at file:///android_asset/www/plugins/org.apache.cordova.file/www/requestFileSystem.js:53:25
D/CordovaLog( 1286):     at success (file:///android_asset/www/plugins/org.apache.cordova.file/www/fileSystems-roots.js:40:13)
D/CordovaLog( 1286):     at Object.cordova.callbackFromNative (file:///android_asset/www/cordova.js:293:54)
D/CordovaLog( 1286):     at processMessage (file:///android_asset/www/cordova.js:1054:21)
D/CordovaLog( 1286):     at Function.androidExec.processMessages (file:///android_asset/www/cordova.js:1091:13)
D/CordovaLog( 1286):     at pollOnce (file:///android_asset/www/cordova.js:956:17)
D/CordovaLog( 1286):     at pollOnceFromOnlineEvent (file:///android_asset/www/cordova.js:946:5)
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1061 : processMessage failed: Message: S01 File1368220044 [{"fullPath":"\/","filesystemName":"temporary","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.phonegap.helloworld\/cache\/","filesystem":0,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"persistent","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.phonegap.helloworld\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"content","isDirectory":true,"nativeURL":"content:\/\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"files","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.phonegap.helloworld\/files\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"documents","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.phonegap.helloworld\/files\/Documents\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"cache","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.phonegap.helloworld\/cache\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"root","isDirectory":true,"nativeURL":"file:\/\/\/","filesystem":1,"isFile":false,"name":""}]
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 43 : run.html
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 43 : run.html
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
I/CordovaLog( 1286): Changing log level to DEBUG(3)
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1169 : deviceready has not fired after 5 seconds.
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1162 : Channel not fired: onDOMContentLoaded
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 43 : main.html
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1059 : processMessage failed: Error: ReferenceError: fileSystem is not defined
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1060 : processMessage failed: Stack: ReferenceError: fileSystem is not defined
D/CordovaLog( 1286):     at gotFS (file:///android_asset/www/js/index.js:306:5)
D/CordovaLog( 1286):     at file:///android_asset/www/plugins/org.apache.cordova.file/www/requestFileSystem.js:53:25
D/CordovaLog( 1286):     at success (file:///android_asset/www/plugins/org.apache.cordova.file/www/fileSystems-roots.js:40:13)
D/CordovaLog( 1286):     at Object.cordova.callbackFromNative (file:///android_asset/www/cordova.js:293:54)
D/CordovaLog( 1286):     at processMessage (file:///android_asset/www/cordova.js:1054:21)
D/CordovaLog( 1286):     at Function.androidExec.processMessages (file:///android_asset/www/cordova.js:1091:13)
D/CordovaLog( 1286):     at pollOnce (file:///android_asset/www/cordova.js:956:17)
D/CordovaLog( 1286):     at pollOnceFromOnlineEvent (file:///android_asset/www/cordova.js:946:5)
D/CordovaLog( 1286): file:///android_asset/www/cordova.js: Line 1061 : processMessage failed: Message: S01 File476869768 [{"fullPath":"\/","filesystemName":"temporary","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.phonegap.helloworld\/cache\/","filesystem":0,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"persistent","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.phonegap.helloworld\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"content","isDirectory":true,"nativeURL":"content:\/\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"files","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.phonegap.helloworld\/files\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"documents","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.phonegap.helloworld\/files\/Documents\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"cache","isDirectory":true,"nativeURL":"file:\/\/\/data\/data\/com.phonegap.helloworld\/cache\/","filesystem":1,"isFile":false,"name":""},{"fullPath":"\/","filesystemName":"root","isDirectory":true,"nativeURL":"file:\/\/\/","filesystem":1,"isFile":false,"name":""}]
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired
D/CordovaLog( 1286): file:///android_asset/www/js/index.js: Line 164 : code: 3
D/CordovaLog( 1286): message: Timeout expired

我使用phonegap build命令构建应用程序,然后模拟它,但我不知道它为什么不保存任何东西。我也在/data/user/0/com.phonegap.helloworld目录中找不到任何新数据。

0 个答案:

没有答案