我正在使用android nexus 9的仿真进行测试。
我为文本运行此代码,它可以工作:
var filePath = cordova.file.dataDirectory + "files/newFile.txt";
window.resolveLocalFileSystemURL(filePath, gotFileEntry, fail);
function gotFileEntry(fileEntry) {
console.log("gotFileEntry: "+fileEntry.name); //"newFile.txt"
console.log("fileEntry fullpath: "+fileEntry.fullPath); //"/newFile.txt" - cause for concern?
fileEntry.file(gotFile, fail);
}
function gotFile(file){
console.log("Got the File");
console.log("Type: " + file.type); //text/plain
console.log("Path: " + file.fullPath); //undefined
//path wasn't defined, manually set it
file.fullPath = fullPath = cordova.file.dataDirectory + 'files/' + file.name;
console.log("Path: " + file.fullPath); //"file:///data/data/com.ionicframework.myproject123456/files/files/newFile.txt"
console.log("Bytes: " + file.size); //~14 bytes
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log('Reader status "onloadend": '+reader.readyState); //2 (a.k.a loaded)
console.log('Result: '+evt.target.result); //"data:text/plain;base64,c26tZSBmaWx1IGrhdGE=" (success)
console.log('Result: '+reader.result); //"data:text/plain;base64,c26tZSBmaWx1IGrhdGE=" (success)
};
reader.onerror = function(e) {
console.log('Error.code: '+reader.error.code) //unused
console.log('Error.message: '+reader.error.message) //unused
}
console.log("Reader status before: "+reader.readyState); //0
reader.readAsDataURL(file);
console.log("Reader status after: "+reader.readyState); //1
}
所以我将其切换为尝试使用图片。相同的代码,为“mypicture.jpg”切换“newFile.txt”:
var filePath = cordova.file.dataDirectory + "files/mypicture.jpg";
window.resolveLocalFileSystemURL(filePath, gotFileEntry, fail);
function gotFileEntry(fileEntry) {
console.log("gotFileEntry: "+fileEntry.name); //"mypicture.jpg"
console.log("fileEntry fullpath: "+fileEntry.fullPath); //"/mypicture.jpg" - cause for concern?
fileEntry.file(gotFile, fail);
}
function gotFile(file){
console.log("Got the File");
console.log("Type: " + file.type); //image/jpeg
console.log("Path: " + file.fullPath); //undefined
//path wasn't defined, manually set it
file.fullPath = fullPath = cordova.file.dataDirectory + 'files/' + file.name;
console.log("Path: " + file.fullPath); //"file:///data/data/com.ionicframework.myproject123456/files/files/mypicture.jpg"
console.log("Bytes: " + file.size); //~3 mega bytes
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log('Reader status "onloadend": '+reader.readyState); //2 (a.k.a loaded)
console.log('Result: '+evt.target.result); //"null" (FAILURE)
console.log('Result: '+reader.result); //"null" (FAILURE)
};
reader.onerror = function(e) {
console.log('Error.code: '+reader.error.code) //1 (NOT_FOUND_ERR?)
console.log('Error.message: '+reader.error.message) //undefined
}
console.log("Reader status before: "+reader.readyState); //0
reader.readAsDataURL(file);
console.log("Reader status after: "+reader.readyState); //1
}
使用“adb shell”我可以显示文件所在的目录。两个文件都在那里。两个文件的权限和所有者都是相同的。
我的最终目标是将文件作为base64数据流读入,并使用katzer的emailComposer插件将其添加为电子邮件的附件。我希望我也可以在iOS部署中使用这种方法。
我如何错误地读取此图像文件 - 为什么reader.readyState返回null?
提前致谢。
答案 0 :(得分:0)
想出来..这是一个笨蛋的错误 - 当我在调试其他内容时覆盖文件时,图像的权限被更改了。