Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用Google Drive API上传文件\u URI:插入文件_Android_Mobile_Cordova_Google Drive Api - Fatal编程技术网

Android 如何使用Google Drive API上传文件\u URI:插入文件

Android 如何使用Google Drive API上传文件\u URI:插入文件,android,mobile,cordova,google-drive-api,Android,Mobile,Cordova,Google Drive Api,在Android上,我试图使用Google Drive API:Insert File上传Cordova/Phonegap getPicture()的输出。有没有办法使用文件URI而不是数据URL(base64)来实现这一点 我首先尝试了Camera.DestinationType.DATA\u URL,但它没有像预期的那样返回Base64数据,它只是返回了与FILE\u URI相同的内容。所以现在我想弄清楚如何将文件URI传递给Google驱动器插入文件(需要Base64)。有没有办法将文件U

在Android上,我试图使用Google Drive API:Insert File上传Cordova/Phonegap getPicture()的输出。有没有办法使用文件URI而不是数据URL(base64)来实现这一点

我首先尝试了Camera.DestinationType.DATA\u URL,但它没有像预期的那样返回Base64数据,它只是返回了与FILE\u URI相同的内容。所以现在我想弄清楚如何将文件URI传递给Google驱动器插入文件(需要Base64)。有没有办法将文件URI转换为Base64

科尔多瓦代码:

navigator.camera.getPicture(onSuccess, onFail,
    { quality: 50, destinationType: Camera.DestinationType.FILE_URI });

function onSuccess(imageURI) {
    var image = document.getElementById('myImage');
    image.src = imageURI;

    // need to do something like this:
    var fileData = ConvertToBase64(imageURI);
    insertFile(fileData);
}
谷歌硬盘代码:

/**
 * Insert new file.
 *
 * @param {File} fileData File object to read data from.
 * @param {Function} callback Function to call when the request is complete.
 */
function insertFile(fileData, callback) {
  const boundary = '-------314159265358979323846';
  const delimiter = "\r\n--" + boundary + "\r\n";
  const close_delim = "\r\n--" + boundary + "--";

  var reader = new FileReader();
  reader.readAsBinaryString(fileData);
  reader.onload = function(e) {
    var contentType = fileData.type || 'application/octet-stream';
    var metadata = {
      'title': fileData.fileName,
      'mimeType': contentType
    };

    var base64Data = btoa(reader.result);
    var multipartRequestBody =
        delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(metadata) +
        delimiter +
        'Content-Type: ' + contentType + '\r\n' +
        'Content-Transfer-Encoding: base64\r\n' +
        '\r\n' +
        base64Data +
        close_delim;

    var request = gapi.client.request({
        'path': '/upload/drive/v2/files',
        'method': 'POST',
        'params': {'uploadType': 'multipart'},
        'headers': {
          'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
        },
        'body': multipartRequestBody});
    if (!callback) {
      callback = function(file) {
        console.log(file)
      };
    }
    request.execute(callback);
  }
}

是的你可以

目标类型
指定为
文件URI
本身,在imagedata中,您将获得图像文件URI。将其放置在图像标记中,然后将其放置在
HTML5画布
中,画布有一个名为toDataURL的方法,您可以在其中获得相应图像的base64

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');
                });

    }
函数成功(imageData)
{

var$img=$('感谢Arun为我指明了正确的方向。我最终使用了这个基于


我意识到这有点过时了,但现在看起来你可以在phoneGap中使用
FileReader

我还没有测试过这个,但是类似的东西也应该可以工作,没有画布黑客

[编辑-测试并修订以下代码。适用于我: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);

嗯,那很有趣。我有几个问题。1)canvas.toDataURL的输出是对我的原始jpeg进行重新采样吗?还是完全相同?2)我想这只适用于图像,而不适用于其他类型的文件?1)是的…您将获得原始图像的base64…两者都是相同的…没有问题..2)我只对图像进行了尝试,它可以工作..我认为它不适用于其他文件类型..Altera感谢Arun,第五种方法是使用Phonegap的文件传输API。我不知怎么地无法运行您的示例。但它让我走上了正确的道路,我能够让它工作!请参见“1)是的…您将获得原始图像的base64…两者都相同…没有问题”我相信从技术上讲,这是对图像进行重新编码。如果原始和输出都是PNG(无损),这不会有任何区别,但如果输出是图像/jpeg,则会有重新编码的损失。这似乎是更好的选择,尤其是远离
文档
,感谢您的发布。
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);