【问题标题】:Cordova fileReader works for text, but returns null for jpgsCordova fileReader 适用于文本,但对于 jpgs 返回 null
【发布时间】:2016-09-09 00:10:13
【问题描述】:

我正在使用 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?

提前致谢。

【问题讨论】:

  • 3mb 对于 dataURL 来说太大了,较小的图像是否有效?我还会在错误处理程序中console.log(e) 以获取详细信息。
  • @dandavis 计划是使用这种方法最终发送最多 25mb 的 zip 文件——我将不得不研究 dataURL 的最大大小。我没有考虑到数据流可能有最大长度。我不断得到“e”未定义,所以我使用了error.code并在线查找。我是这种语言的新手。
  • 根据这些文章我认为尺寸不会有问题:link

标签: javascript angularjs cordova filereader


【解决方案1】:

想通了..这是一个愚蠢的错误-当我在调试其他东西时覆盖了文件时,图像的权限被更改了。

【讨论】:

  • 你给予了什么样的许可。请解释一下...我正在从图库访问相同的图像我可以访问它但是我从文件管理器访问的相同图像在这给了我reader.result = null
猜你喜欢
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 2011-01-22
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多