Search code examples
javascriptandroidcordovaionic-frameworkcordova-plugins

Ionic error (Camera and File transfer plugin)


I am developing a mobile application and I getting an error when the user try to change the picture profile, the error after do a lot of debugging is not in the camera is in the file transfer function when I try to send the image to the server, there is no error code or message, simply the application stop work, this is the code:

$scope.addImage = function (option) {
var options = {
  quality: 75,
  targetWidth: 300,
  destinationType: Camera.DestinationType.DATA_URL,
  targetHeight: 300,
  saveToPhotoAlbum: false,
  correctOrientation: true
};

if (option == 1) {
  options.sourceType = Camera.PictureSourceType.CAMERA;
} else {
  options.sourceType = Camera.PictureSourceType.PHOTOLIBRARY;
}

$cordovaCamera.getPicture(options).then(function (imageData) {
  console.log("IMAGE DATA");
  console.log(imageData);
  //alert("SUCCESS");
  $scope.user.image = "data:image/jpeg;base64," + imageData;
  console.log(JSON.stringify($scope.user));
  $scope.savePicture();
}, function (err) {
  alert("ERRROR");
  alert(JSON.stringify(err));
  // An error occured. Show a message to the user
});
};



$scope.savePicture = function () {
  var options = {
    fileKey: "avatar",
    fileName: "image.jpg",
    chunkedMode: false,
    mimeType: "image/jpeg",
    headers: {
      Authorization: "Bearer " + $auth.getToken()
    }
  };

  $cordovaFileTransfer.upload(api.getApi()+"user/updatephoto", $scope.user.image, options).then(function (result) {
    console.log("SUCCESS: " + JSON.stringify(result.response));
}, function (err) {
    console.log("ERROR: " + JSON.stringify(err));
    alert("ERROR: " + JSON.stringify(err));
}, function (progress) {
    // constant progress updates
});

};

Thank in advice for your help


Solution

  • I had to debug the entire application including the cat log of android and I found the problem.

    The problem was that I am using Ionic with crosswalk and the version of FileTransfer that I was using does not support crosswalk.

    I fix this problem just installing the last version of file transfer:

    cordova-plugin-file-transfer 1.2.1 "File Transfer"