Phonegap - 通过filetransfer发送时,相机的图像URL错误了吗?

时间:2013-01-02 21:35:27

标签: javascript android cordova

所以看来我偶然发现了一个与phonegap 2.2.0非常奇怪的问题。

我正在尝试拍照,并通过filetransfer插件发送,就像这样:

if(navigator.camera) {
        navigator.camera.getPicture(function(imageURI){
            console.log('captured image = '+imageURI);
            $('#photoConfirmation .image-preview').attr('src', imageURI);
            self.photoURI = imageURI;
            $('#photoConfirmation').show().simpledialog2({
                'mode' : 'bool',
                'prompt' : '',
                'useModal': true,
                'zindex':1001,                    
                'callbackClose': function(e){
                    $('#photoConfirmation').hide();
                }
            });
        }, 
        function(message){
            alert('Failed to get picture: ' + message);
        }, {
            sourceType:1, 
            quality: 50, 
            destinationType:1
        });
    } else {
        alert('Camera is not supported on this device.');
    }    

 var options = new FileUploadOptions();
    options.fileKey="files[]";
    options.fileName = 'image_something'.jpg';
    options.mimeType="image/jpeg";
    options.chunkedMode = false;

    var params = new Object();
    params.extraData= 'object-' + anObject.id;

    options.params = params;
    var ft = new FileTransfer();
    ft.upload(imageURI, "http://someupload.com/destination/url", function(r){alert('Finished upload!');}, function(error){console.log(error);alert('Error uploading image with code: ' +error.code)}, options);

问题是,这会在预览中显示,但不会上传!这张照片肯定会被捕获,因为我在图像预览中看到它......我可以发誓我之前做过类似的事情。有谁看到我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

Cordova API Documentation上有一个FileTransfer插件的参考实现(使用摄像头拍摄的照片),与你的实现有很多不同之处。通过阅读代码,您可以在捕获照片之前启动文件传输(并创建imageURI)。您还使用sourceTypedestinationType的数值常量。虽然这些最不可能导致您遇到的问题,但正确的形式是:

destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY

以下是一些示例,其中包含一些小错字修复(我假设fileName中的额外'不是您的代码的一部分)应该是什么样的工作版本:

if (navigator.camera) {
  navigator.camera.getPicture(

  function (imageURI) {
    console.log('captured image = ' + imageURI);
    $('#photoConfirmation .image-preview').attr('src', imageURI);
    self.photoURI = imageURI;
    $('#photoConfirmation').show().simpledialog2({
      'mode': 'bool',
      'prompt': '',
      'useModal': true,
      'zindex': 1001,
      'callbackClose': function (e) {
        $('#photoConfirmation').hide();
        var options = new FileUploadOptions();
        options.fileKey = "files[]";
        options.fileName = 'image_something.jpg';
        options.mimeType = "image/jpeg";
        options.chunkedMode = false;
        var params = new Object();
        params.extraData = 'object - ' + anObject.id;
        options.params = params;
        var ft = new FileTransfer();
        ft.upload(imageURI, "http://someupload.com/destination/url", function (r) {
          alert('Finished upload!');
        }, function (error) {
          console.log(error);
          alert('Error uploading image with code: ' + error.code)
        }, options);
      }
    });
  },

  function (message) {
    alert('Failed to get picture: ' + message);
  }, {
    sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,
    quality: 50,
    destinationType: navigator.camera.DestinationType.FILE_URI
  });
} else {
  alert('Camera is not supported on this device.');
}