PhoneGap/Cordova BlackBerry FileSystem.root always returns SD card?

梦想的初衷 提交于 2019-12-12 08:39:12

问题


I have two applications I'm loading on a BlackBerry, one is a native application, the other is a PhoneGap / Cordova based application.

These two applications share a settings file: file:///store/home/user/myfile.txt

To access this file via Cordova, I call:

fileSystem.root.getFile("home/user/myfile.txt", null, gotFileEntry, fail);

However, on some devices fileSystem.rootreturns the SDCard directory, not the internal memory where the file is stored, so I get a FileNotFound error.

I've tried calling fileSystem.root.getParent(gotParent, fail);, hoping to get the root of the filesystem, then get the file from internal memory like so:

parentDir.getFile("store/home/user/myfile.txt", null, gotFileEntry, fail);

But that doesn't work either, I still receive a file not found error.

How can I get the root directory of the internal memory every time, using PhoneGap/Cordova?


回答1:


What version of Cordova do you use?
Cordova File-API is supported since BlackBerry WebWorks (OS 5.0 and higher).

Am I right in the assumption that you only want to read (and write) that file?
If so you can try to use the Cordova File-Reader and the Cordova File-Writer.

FileReader

function win(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        console.log("read success");
        console.log(evt.target.result);
    };
    reader.readAsText(file);
};

var fail = function(evt) {
    console.log(error.code);
};

entry.file(win, fail);

FileWriter

function win(writer) {
    writer.onwrite = function(evt) {
        console.log("write success");
    };
    writer.seek(writer.length);
    writer.write("appended text");
};

var fail = function(evt) {
    console.log(error.code);
};

entry.createWriter(win, fail);

Otherwise give that snippet a try (code is of an old project but worked at that time)

<script type="text/javascript" charset="utf-8" src="css-js/phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
//
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

}

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

}

function gotFileEntry(fileEntry) {

    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
    writer.onwrite = function(evt) {
        console.log("write success");
    };
    writer.write("some sample text");

I hope I could help you, best regards F481



来源:https://stackoverflow.com/questions/13664102/phonegap-cordova-blackberry-filesystem-root-always-returns-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!