PhoneGap FileReader/readAsDataURL Not Triggering Callbacks

。_饼干妹妹 提交于 2020-02-02 10:12:22

问题


I am using PhoneGap Build to build an iOS v7.1+ application and using weinre to debug. I am using the media-capture plugin and file API to capture a video in an attempt get its base64 representation. I can get the video recorder to open, take a video, and return the file path. I then use resolveLocalFileSystemURL() to get a file object which readAsDataURL() requires. The problem is FileReader is never calling the onloadend callback.

I have been poking around all day. Putting console.log()'s everywhere I could think of. I checked to make sure the iOS version is supported. Every variable is what I expect yet the callback is simply not being called. I have also tried setting up all the other callbacks but none of them ever get called, either. I have tried replacing readAsDataURL() with readAsText() but I still get bupkis. I have tried waiting up to five minutes since I figured an asynchronous call may take a bit but still nothing.

Below is my code. Below that is the console output.

var elements = new Object();
elements["video"] = $("#window_incident_create > .video > source")[0];

navigator.device.capture.captureVideo(
    function(files) {
        for ( var i in files ) {
            var file = files[i];

            var name = file.name;
            var path = file.fullPath;
            if ( path.indexOf("/private") === 0 )
                path = "file://" + path.substr(8);
            else
                path = "file://" + path;
            var type = file.type;
            var lastModifiedDate = file.lastModifiedDate;
            var size = file.size;

            var reader = new FileReader();
            reader.onloadend = function(event) {
                console.log(3);
                elements["video"].type = type;
                elements["video"].src = "data:" + type + ";base64," + event.target.result;
                console.log(4);
            };

            window.resolveLocalFileSystemURL(
                path,
                function(entry) {
                    console.log(1, entry.nativeURL);
                    reader.readAsDataURL(entry);
                    console.log(2);
                },
                function(error) {
                    console.log("0-0", error);
                }
            );
        }
    },
    function(error) {
        console.log("0-1", error);
    },
    {
        limit:  1
    }
);

1 "file:///var/mobile/Applications/AB239984-FB9F-43C0-B699-3596AC8A43A8/tmp/capture/capturedvideo.MOV"

2


回答1:


Reorganize your code a bit. Put the reader initialisation and onloadend callback inside the resolveLocalFileSystemURL success callback.

Something like this:

var elements = new Object();
elements["video"] = $("#window_incident_create > .video > source")[0];

navigator.device.capture.captureVideo(
    function(files) {
        for ( var i in files ) {
            var file = files[i];

            var name = file.name;
            var path = file.fullPath;
            if ( path.indexOf("/private") === 0 )
                path = "file://" + path.substr(8);
            else
                path = "file://" + path;
            var type = file.type;
            var lastModifiedDate = file.lastModifiedDate;
            var size = file.size;

            window.resolveLocalFileSystemURL(
                path,
                function(entry) {
                    console.log(1, entry.nativeURL);
                    var reader = new FileReader();
                    reader.onloadend = function(event) {
                        console.log(3);
                        elements["video"].type = type;
                        elements["video"].src = "data:" + type + ";base64," + event.target.result;
                        console.log(4);
                    };
                    reader.readAsDataURL(entry);
                    console.log(2);
                },
                function(error) {
                    console.log("0-0", error);
                }
            );
        }
    },
    function(error) {
        console.log("0-1", error);
    },
    {
        limit:  1
    }
);


来源:https://stackoverflow.com/questions/25750561/phonegap-filereader-readasdataurl-not-triggering-callbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!