如何在Ionic 1中获取文件路径

时间:2018-04-01 10:27:58

标签: ionic-framework

我在Ionic 1中的文件uploding期间遇到问题。我没有得到确切的文件路径而不是图片网址我得到了很长的网址所以请检查并帮助我。

$scope.selectPhoto = function(){
        var options = {
                    quality: 50,
                    destinationType: Camera.DestinationType.DATA_URL,
                    sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
                    targetWidth: 400,
                   targetHeight: 500
        };

              $cordovaCamera.getPicture(options).then(
                function(imageData) {
                        $ionicLoading.show({template: 'Image Selected...', duration:1000});
                            $scope.imageURI = "data:image/jpeg;base64," + imageData;
                            $scope.imgeCaptured = imageData;

                             $scope.imge = {
                              "action":"create",
                              "id":"",
                              "plantId":$localStorage.plantid,
                              "machineId":"",
                              "attachmentPath":$scope.imgeCaptured
                            }
                             alert ($scope.imge);   
                },
                function(err){
                    $ionicLoading.show({template: 'Errore in Image Selection...', duration:500});
                })                    
    }

1 个答案:

答案 0 :(得分:0)

捕捉或挑选图像

 $scope.loadImage = function() {
  var options = {
    title: 'Select Image Source',
    buttonLabels: ['Load from Library', 'Use Camera'],
    addCancelButtonWithLabel: 'Cancel',
    androidEnableCancelButton : true,
  };
  $cordovaActionSheet.show(options).then(function(btnIndex) {
    var type = null;
    if (btnIndex === 1) {
      type = Camera.PictureSourceType.PHOTOLIBRARY;
    } else if (btnIndex === 2) {
      type = Camera.PictureSourceType.CAMERA;
    }
    if (type !== null) {
      $scope.selectPicture(type);
    }
  });
}; 

此代码将在选择时调用,图像将移至本地app目录,图像名称将在此处获取

$scope.selectPicture = function(sourceType) {
  var options = {
    quality: 100,
    destinationType: Camera.DestinationType.FILE_URI,
    sourceType: sourceType,
    saveToPhotoAlbum: false
  };

  $cordovaCamera.getPicture(options).then(function(imagePath) {
    // Grab the file name of the photo in the temporary directory
    var currentName = imagePath.replace(/^.*[\\\/]/, '');

    //Create a new name for the photo
    var d = new Date(),
    n = d.getTime(),
    newFileName =  n + ".jpg";

    // If you are trying to load image from the gallery on Android we need special treatment!
    if ($cordovaDevice.getPlatform() == 'Android' && sourceType === Camera.PictureSourceType.PHOTOLIBRARY) {
      window.FilePath.resolveNativePath(imagePath, function(entry) {
        window.resolveLocalFileSystemURL(entry, success, fail);
        function fail(e) {
          console.error('Error: ', e);
        }

        function success(fileEntry) {
          var namePath = fileEntry.nativeURL.substr(0, fileEntry.nativeURL.lastIndexOf('/') + 1);
          // Only copy because of access rights
          $cordovaFile.copyFile(namePath, fileEntry.name, cordova.file.dataDirectory, newFileName).then(function(success){
            $scope.image = newFileName;
          }, function(error){
            $scope.showAlert('Error', error.exception);
          });
        };
      }
    );
    } else {
      var namePath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
      // Move the file to permanent storage
      $cordovaFile.moveFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(function(success){
        $scope.image = newFileName;
      }, function(error){
        $scope.showAlert('Error', error.exception);
      });
    }
  },
  function(err){
    // Not always an error, maybe cancel was pressed...
  })
};

此方法将为您提供图像路径

// Returns the local path inside the app for an image
$scope.pathForImage = function(image) {
  if (image === null) {
    return '';
  } else {
    return cordova.file.dataDirectory + image;
  }
};

有关详细信息,请查看此link