How to upload FILE_URI using Google Drive API: Insert File

前端 未结 3 1156
深忆病人
深忆病人 2021-01-03 05:52

On Android, I\'m trying to upload the output from Cordova/Phonegap getPicture() using Google Drive API: Insert File. Is there a way to do this using the FILE_URI instead of

相关标签:
3条回答
  • 2021-01-03 06:19

    I realize this is a bit old - but it now looks like you can use a FileReader in phoneGap.

    I haven't tested this yet, but something like this should also work, without the canvas hack.

    [EDIT - tested and revised the code below. works for me :D ]

    var cfn = function(x) { console.log(x) };
    var cameraOps = { quality: 50, destinationType: Camera.DestinationType.FILE_URI };
    navigator.camera.getPicture(function(imagePath) {
        window.resolveLocalFileSystemURL(imagePath, function(fileEntry) {
            fileEntry.file(function (file) {
                var reader = new FileReader();
                reader.onloadend = function(evt) {
                    console.log("read success!!!");
                    console.log(evt.target.result);
                };
                reader.readAsDataURL(file);
            }, cfn);
        }, cfn);
    }, cfn);
    
    0 讨论(0)
  • 2021-01-03 06:21

    Yes you can....

    Specify Destination Type as FILE_URI itself and in imagedata you will be getting the images file uri place it in a image tag and then place it inside HTML5 canvas and canvas has one method called toDataURL where you will be able to get the base64 of the corresponding image.

    function onSuccess(imageData)
         {
    
                    var $img = $('<img/>');
                    $img.attr('src', imageData);
                    $img.css({position: 'absolute', left: '0px', top: '-999999em', maxWidth: 'none', width: 'auto', height: 'auto'});
                    $img.bind('load', function() 
                    {
                        var canvas = document.createElement("canvas");
                        canvas.width = $img.width();
                        canvas.height = $img.height();
                        var ctx = canvas.getContext('2d');
                        ctx.drawImage($img[0], 0, 0);
                        var dataUri = canvas.toDataURL('image/png');
    
                    });
                    $img.bind('error', function() 
                    {
                        console.log('Couldnt convert photo to data URI');
                    });
    
        }
    
    0 讨论(0)
  • 2021-01-03 06:23

    Thanks to Arun for pointing me in the right direction. I ended up using this javascript function, which was based off http://jsfiddle.net/jasdeepkhalsa/L5HmW/

    function getBase64Image(imgElem) {
    // imgElem must be on the same server otherwise a cross-origin error will be thrown "SECURITY_ERR: DOM Exception 18"
        var canvas = document.createElement("canvas");
        canvas.width = imgElem.clientWidth;
        canvas.height = imgElem.clientHeight;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(imgElem, 0, 0);
        var dataURL = canvas.toDataURL("image/jpeg");
        dataURL = dataURL.replace(/^data:image\/(png|jpg|jpeg);base64,/, "");
        return dataURL;
    }
    
    0 讨论(0)
提交回复
热议问题