我是初学者,在使用javascript和phonegap时遇到奇怪的问题。这是我正在处理的javascript:
var app = {
// Application Constructor
initialize : function() {
alert("hpslisdf");
this.bindEvents();
},
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 explicity call 'app.receivedEvent(...);'
onDeviceReady : function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent : function(id) {
// alert("inside received");
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;');
alert("received event");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
alert("after filesystem");
console.log('Received Event: ' + id);
}
};
function gotFS(fileSystem) {
// var reader = fileSystem.root.createReader();
// reader.readEntries(gotList, fail);
var sdcard = fileSystem.root;
// alert(sdcard.value);
sdcard.getDirectory('dcim/camera', {
create : false
}, function(dcim) {
// alert(dcim.value);
var directoryReader = dcim.createReader();
// alert("hs");
directoryReader.readEntries(function(entries) {
for (var i = 0; i <1 ; i++) {
//entries.length
// alert(entries.length);
entries[i].file(function(f) {
// alert(entries[i]);
var reader = new FileReader();
reader.onloadend = function(evt) {
// alert("xxxxxx");
var url = evt.target.result;// base64 data uri
// alert("now");
// var img = $('<img id="dynamic>'); //Equivalent:
// $(document.createElement('img'))
// var img=$('#dynamic');
// img.attr('value','')
// img.attr('src', url);
// img.appendTo('#imagediv');
// $('#imagediv').html('hiiiii').show();
// imagediv.attr('value','hiiiii');
// alert("hello");
var imagediv = document.getElementById("imagediv");
imagediv.innerHTML = "hiytrud";
// var elem = document.createElement("img");
//elem.src = url;
//imagediv.appendChild(elem);
// alert(url);
// console.log(url)
// reader.abort();
};
reader.readAsDataURL(f);
},
function(error) {
console("Unable to retrieve file properties: "
+ error.code);
});
}
}, function(e) {
console.log(e.code);
});
}, function(error) {
console.log(error.code);
});
}
function fail(evt) {
alert("fail");
console.log(evt.target.error.code);
}
现在我面临的第一个问题是我看到大部分警报两次,但app.initialize方法只在我的html代码中被调用一次。并且没有调用gotFS()。现在大多数人会说问题出在以下几行:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
但我添加了fileSystem
和FileTransfer
个插件。此外,控件到达下一行,我可以看到下一行的警报,即“文件系统之后”,但此警报只出现一次。并且getFS方法或fail方法根本没有加载。我不知道我做错了什么请帮助。
答案 0 :(得分:0)
可能不起作用的两个明显原因是:
你能为回调提供代码,并确保可以在没有来自receiveEvent的错误的情况下调用它吗?