How to get image and audio from $cordovaCapture for playback and base64 string


#1

I’m trying to get images and audio from $cordovaCapture
But I’m stuck after data is returned from captureImage / captureAudio

Can’t use the fullPath as I can see from the cordova serving Terminal is 404

404 /var/mobile/Containers/Data/Application/91036FB8-4195-4902-99E1-ED943E738DB3/tmp/photo_001.jpg

When I try to use open the file with fullPath with $cordovaFile, I end in error code 1 which is file not found.

Here’s the basic code

.controller('MyCtrl', [
  '$scope', '$cordovaCapture', '$cordovaCamera', 'FileService',
  function($scope, $cordovaCapture, $cordovaCamera, FileService) {
    $scope.imgs = [];
    $scope.ados = [];
    $scope.debug = '';

    $scope.captureAudio = function() {
      var options = {
        limit: 3,
        duration: 20
      };

      $cordovaCapture.captureAudio(options).then(function(audioData) {
        // Success! Audio data is here
        var i, path, len, prop;
        for (i = 0, len = audioData.length; i < len; i += 1) {
          // path = mediaFiles[i].fullPath;
          // do something interesting with the file
          for (prop in audioData[i]) {
            $scope.debug += prop + ':' + audioData[i][prop] + '\n';
          }
          $scope.ados.push(audioData[i]);
        }
      }, function(error) {
        // An error occured. Show a message to the user
      });
    };

    $scope.captureImage = function() {
      var options = {
        limit: 3
      };

      var prop, str='';
      $cordovaCapture.captureImage(options).then(function(imageData) {
        // Success! Image data is here
        var i, path, len, src;
        len = imageData.length;
        $scope.debug = 'imageData.length: ' + len;
        for (i = 0; i < len; i++) {
          path = imageData[i].fullPath;
          name = imageData[i].name;
          src = FileService.readFile(path);
          $scope.imgs.push({
            fullPath: path,
            name: name,
            src: src
          });
        }
      }, function(error) {
        // An error occured. Show a message to the user
        str = 'code: ' + error.code + '\n' + 'message: ' + error.message + '\n';
        alert(str);
      });
    };
  }
])

.service('FileService', [
  '$cordovaFile',
  function($cordovaFile) {
    this.readFile = function(path) {
      // Reads a file as TEXT
      var str = '', prop;
      $cordovaFile.readFile(path).then(function(result) {
        // Success!
        str = 'Read File:\n';
        for (prop in result){
          str += prop + " : " + result[prop] + "\n";
        }
          alert(str);
        // return result;
        return 'data:image/jpeg;base64,' + result;
      }, function(error) {
        // An error occurred. Show a message to the user
        str = 'code: ' + error.code + '\n' + 'message: ' + error.message + '\n';
        alert(str);
        return undefined;
        // return str;
      });
    };
  }
])