我遇到令人沮丧的问题,让cordova将文件写入Android设备。
根据日志,一切正常,并且插件方法响应成功等,但当我去搜索文件时,他们无处可寻。
目前我只是使用新的phonegap测试应用程序,我按照以下指南并逐字使用了他们的示例代码。根据正在执行的日志安装插件。
http://docs.phonegap.com/en/edge/cordova_file_file.md.html
我希望该文件显示在/android/data/com.testapp.myapp/files
这是我的测试代码:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, app.gotFS, app.fail);
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
},
gotFS: function(fileSystem) {
fileSystem.root.getFile("testFile.txt", {create: true, exclusive: false}, app.gotFileEntry, app.fail);
},
gotFileEntry: function(fileEntry) {
fileEntry.createWriter(app.gotFileWriter, app.fail);
},
gotFileWriter: function(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");
},
fail: function() {
alert("failed");
}
};
以下是来自logCat的日志条目,显示它已启动:
09-26 07:24:37.991 I/chromium( 2027): [INFO:CONSOLE(49)] "Received
Event: deviceready", source: file:///android_asset/www/js/index.js (49)
09-26 07:24:38.591 D/TEST ( 2027): cdvfile://localhost/persistent/testFile.txt: 16
09-26 07:24:39.063 I/chromium( 2027): [INFO:CONSOLE(62)] "contents of file now 'some sample text'", source: file:///android_asset/www/js/index.js (62)
09-26 07:24:39.075 D/TEST ( 2027): cdvfile://localhost/persistent/testFile.txt: 15
09-26 07:24:39.155 I/chromium( 2027): [INFO:CONSOLE(65)] "contents of file now 'some sample'", source: file:///android_asset/www/js/index.js (65)
09-26 07:24:39.363 I/chromium( 2027): [INFO:CONSOLE(69)] "contents of file now 'some different text'", source: file:///android_asset/www/js/index.js (69)
对于为什么会发生这种情况的任何见解都会很棒。
谢谢!
答案 0 :(得分:0)
不要让它变得如此复杂,这是将数据写入文件的代码。存储在'decdata'变量中的数据。不要忘记为根目录中的文件夹更改'somefolder',并将'datafilename'更改为存储数据的文件。 你还应该添加文件插件。
function start(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, writedata, fail);
}
var decdata;
function writedata(fileSystem) {
fileSystem.root.getDirectory("<<somefolder>>", { create: true }, gotDir);
}
function gotDir(dirEntry) {
dirEntry.getFile(<<datafilename>>, { create: true, exclusive: false }, gotFile);
}
function gotFile(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwriteend = function (evt) {
writer.truncate(1);
writer.onwriteend = function (evt) {
writer.seek(0);
writer.write(decdata);
writer.onwriteend = function (evt) {
}
};
};
writer.write("some sample text");
}
function fail(error) {
alert('error' + error.code);
}