1

I am creating a mobile app and i have encounted a problem on blackberry phones (most of my users are using blackberry). I am using phonegap api.

The problem is that i am downloading a string which i am saving in localstorage on the phone's browser and when the string gets over x million chars localstorage is no longer able to store the string. I need at least 10 times more space than localstorage provides me with.

I have tried saving the string as a text file instead and followed the docs provided by phonegap but could not manage to read or write to file so i gave up (i think this is the only way for me to do this). Also have looked into compressing the string but like i said before i need over 10 times the space and i dont see how a 10 char string can be transformed into a 1 char string.

Code for file reader and writer (problem is it never finds or creates the file, getfile() fails for reading or writing and returns error code 1 - file_not_found):

function OnLoadSignInCheck(){

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

}

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

function gotFS(fileSystem) {
alert("3");
    fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

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

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

function fail(error) {
    alert(error.code);
}

function reader() {
alert("1");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFSr, fail);
}

function gotFSr(fileSystem) {
    alert("2");
    fileSystem.root.getFile("readme.txt", null, gotFileEntryr, fails);
}

function gotFileEntryr(fileEntry) {
    alert("3");
    fileEntry.file(gotFiler, fails);
}

function gotFiler(file){
    alert("4");
    readDataUrl(file);
    readAsText(file);
}

function readDataUrl(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        alert("Read as data URL");
        alert(evt.target.result);
    };
    reader.readAsDataURL(file);
}

function readAsText(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        alert("Read as text");
        alert(evt.target.result);
    };
    reader.readAsText(file);
}

function fails(evt) {
    alert(evt.target.error.code);
}
4

1 回答 1

0

解决的文件路径应该是“file:///store/home/user/documents/readme.txt”的形式。然而,这给我带来了我的下一个问题——我如何让用户选择一个文件路径,因为它对于每个用户或至少对于 bb、iphone、android 用户来说都是不同的?

于 2013-06-12T12:10:33.877 回答