私たちのアプリでは、phonegap ファイルシステムを使用してメッセージの詳細を取得するためにファイルを読み込んでいます。ほとんどの場合、Android で動作しますが、Windows Phone 7 では、同じファイルを読み取ると、ファイル システムが「null または undefined」になることがよくあります。参考までに私のコードを以下に貼り付けます。
this.getMessageContent = function(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onProcessFailure);
};
function onFileSystemSuccess(fileSystem){
fileSystem.root.getFile("/app/www/Notifications.txt", null, gotFileEntry, onProcessFailure);
console.log("File system name"+fileSystem.name);
console.log("File root name"+fileSystem.root.name);
}
function gotFileEntry(fileEntry) {
fileEntry.file(readFileContent, onProcessFailure);
}
function readFileContent(file) {
console.log("File Size-->"+file.size);
console.log("File url-->"+file.fullPath);
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as text");
var contentLength = evt.target.result;
if(evt.target.result){
console.log("File Content Non-Empty-->"+evt.target.result);
}else{
console.log("File Content Empty-->"+evt.target.result);
}
};
reader.readAsText(file);
}
function onProcessFailure(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
console.log('Notification File Related Error: ' + msg);
}
上記のコードは正常に動作する場合もあれば、次のエラーがスローされる場合もあります
Log:"Error in error callback: File672824422 = TypeError: Unable to get value of the property 'getFile': object is null or undefined"
phonegap fileSystem オブジェクトが頻繁に null または未定義になっています。
関数 this.getMessageContent() を呼び出してファイルを読み取ることをクリックすると、リンクが表示されます。Windows phone7 でファイルシステム オブジェクトが null または undefined になる方法がよくわかりません。