PhoneGap readAsDataURL [英] PhoneGap readAsDataURL

查看:171
本文介绍了PhoneGap readAsDataURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PhoneGap编写了我的第一个Android应用程序,但我对FileReader的文档感到困惑。我需要拍摄一个图像文件,并使用readAsDataURL()方法将其转换为Base64字符串。 从其文档:

I am writing my first Android app using PhoneGap, but I'm a little confused by the documentation for the FileReader. I need to take an image file and convert it to a Base64 string using the readAsDataURL() method. 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);

除了最后一行,我还理解了其他所有内容:entry.file(win,fail) 。没有定义条目,但我认为它是一个FileEntry对象。问题是,我没有太多运气查找关于如何生成FileEntry对象的文档,以及在什么时候我传入一个文件路径。

I understand pretty much all of that except for the last line: entry.file(win, fail). Nowhere is entry defined, but I assume it is a FileEntry object. 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.

推荐答案

好的,终于得到这个工作。可怕的文档在线!我发布我的代码,以防其他人遇到麻烦:

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 () { }
);

这篇关于PhoneGap readAsDataURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆