简体   繁体   English

PhoneGap readAsDataURL

[英]PhoneGap readAsDataURL

I am writing my first Android app using PhoneGap, but I'm a little confused by the documentation for the FileReader. 我正在使用PhoneGap编写我的第一个Android应用程序,但我对FileReader的文档感到有些困惑。 I need to take an image file and convert it to a Base64 string using the readAsDataURL() method. 我需要使用readAsDataURL()方法获取一个图像文件并将其转换为Base64字符串。 From their documentation: 从他们的文件:

function win(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
    console.log("read success");
    console.log(evt.target.result);
};
reader.readAsDataURL(file);
};
var fail = function(evt) {
console.log(error.code);
};
entry.file(win, fail);

I understand pretty much all of that except for the last line: entry.file(win, fail). 除了最后一行,我几乎理解了所有这些:entry.file(win,fail)。 Nowhere is entry defined, but I assume it is a FileEntry object. 没有定义入口,但我认为它是一个FileEntry对象。 The problem is that I have not had much luck finding documentation on how to generate the FileEntry object, and at what point I pass in a file path. 问题是我找不到关于如何生成FileEntry对象的文档,以及在什么时候传递文件路径。

Ok, finally got this to work. 好的,终于有了这个工作。 Horrible documentation online! 在线可怕的文档! I'm posting my code in case others are having trouble: 我发布我的代码以防其他人遇到麻烦:

window.resolveLocalFileSystemURI(filePath,
    // success callback; generates the FileEntry object needed to convert to Base64 string
    function (fileEntry) {
        // convert to Base64 string
        function win(file) {
            var reader = new FileReader();
            reader.onloadend = function (evt) {
                var obj = evt.target.result; // this is your Base64 string
            };
            reader.readAsDataURL(file);
        };
        var fail = function (evt) { };
        fileEntry.file(win, fail);
    },
    // error callback
    function () { }
);

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

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