Copy file doesn't work


#1

Hi all,

I trying to copy a file from local store of my app to another dir… I’m working on a code based from a tutorial that I found on the web.

Now after different time that I try to solve my problem I’m just arrived that the problem can be in this lines

  function copyFile(fileEntry){
    var name = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('/') + 1);
    var newName = makeid() + name;
    window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fileSystem2) {
      fileEntry.copyTo(
        fileSystem2,
        newName,
        onCopySuccess,
        fail);
    },
    fail);
  }

so my problem is that I found the file that was copyed into the dir of my phone: /storage/emulated/0/Android/data/com.ionicframework.mceitalia9483948/cache/

and when I attached this file on an email under attachments the file doesn’t found.

why??

here there is my complete code of the controller

.controller('AccountCtrl', function($scope, $cordovaCamera, $cordovaFile) {
  $scope.settings = {
    enableFriends: true
  };
  $scope.composeEmail = {};
  $scope.images = [];
  $scope.takePicture = function() {
    var options = {
      destinationType : Camera.DestinationType.FILE_URI,
      sourceType : Camera.PictureSourceType.CAMERA, // Camera.PictureSourceType.PHOTOLIBRARY
      allowEdit : false,
      encodingType : Camera.EncodingType.JPEG,
      popoverOptions : CameraPopoverOptions,
    };
    $cordovaCamera.getPicture(options).then(function(imageData) {
      onImageSuccess(imageData);
      function onImageSuccess(fileURI) {
        createFileEntry(fileURI);
      }
      function createFileEntry(fileURI) {
        window.resolveLocalFileSystemURL(fileURI, copyFile, fail);
      }
      function copyFile(fileEntry){
        var name = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('/') + 1);
        var newName = makeid() + name;
        window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fileSystem2) {
          fileEntry.copyTo(
            fileSystem2,
            newName,
            onCopySuccess,
            fail);
        },
        fail);
      }
      function onCopySuccess(entry) {
        $scope.$apply(function () {
          $scope.images.push(entry.nativeURL);
        });
      }
      function fail(error) {
        console.log("fail: " + error.code);
      }
      function makeid() {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        for (var i = 0; i < 5; i++) {
          text += possible.charAt(Math.floor(Math.random() * possible.length));
        }
        return text;
      }
    }, function(err) {
      console.log(err);
    });
  }
  $scope.urlForImage = function(imageName) {
    var name = imageName.substr(imageName.lastIndexOf('/') + 1);
    var trueOrigin = "" + cordova.file.dataDirectory + name;
    return trueOrigin;
  }
  $scope.sendEmail = function() {
    var namefrom = $scope.composeEmail.namefrom;
    var subject = $scope.composeEmail.subject;
    var bodytext = $scope.composeEmail.bodytext;
    var bodytext = "Nome: " + namefrom + "<br><br>Testo: " + bodytext + "<br><br><i>Inviato da Mobile App - MCEItalia</i>";
    if (null != $scope.images) {
      var images = [];
      var savedImages = $scope.images;
      for (var i = 0; i < savedImages.length; i++) {
        images.push("" + $scope.urlForImage(savedImages[i]));
        images[i] = images[i].replace('file://', '');
      }
      window.plugin.email.open({
        to: ["francescopassanante@gmail.com"],
        cc: null,
        bcc: null,
        attachments: images,
        subject: subject,
        body: bodytext,
        isHtml: true,
      }, function() {
        console.log('email view dismissed');
      },
      this);
    }
  }
})