问题
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.root
returns 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