简体   繁体   English

Cordova fileReader适用于文本,但对于jpgs返回null

[英]Cordova fileReader works for text, but returns null for jpgs

I'm using an emulation of an android nexus 9 for testing. 我正在使用android nexus 9的仿真进行测试。

I run this code for text, and it works: 我为文本运行此代码,并且可以正常工作:

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
}

So I switch it to try and use the picture. 因此,我将其切换为尝试使用图片。 Same code, switching "newFile.txt" for "mypicture.jpg": 相同的代码,将“ 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
}

Using "adb shell" I'm able to show the directory where the files are located. 使用“ adb shell”,我可以显示文件所在的目录。 Both files are there. 这两个文件都在那里。 The permissions and owner of both files are the same. 这两个文件的权限和所有者相同。

My end goal is to read in a file as a base64 datastream and add it as an attachment to an email using katzer's emailComposer plugin. 我的最终目标是读取文件作为base64数据流,并使用katzer的emailComposer插件将其添加为电子邮件的附件。 I'm hoping that I can use this method on an iOS deployment also. 我希望我也可以在iOS部署中使用此方法。

How am I reading this image file incorrectly -- why is reader.readyState returning null? 我如何错误地读取此图像文件-为什么reader.readyState返回null?

Thanks in advance. 提前致谢。

弄清楚了..这是一个笨拙的错误-在调试其他内容时改写文件时更改了图像的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM