0

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

我正在尝试拍照,并通过文件传输插件发送,如下所示:

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

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

4

1 回答 1

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.');
}
于 2013-01-02T21:45:55.897 回答