如何使用cordova相机插件获取上传文件的文件大小和文件类型

时间:2017-07-04 13:34:28

标签: cordova ionic-framework cordova-plugins

我想使用cordova相机插件获取上传文件的类型和大小。我使用以下代码

var source='';
if(sourceSelection==0){
   source=Camera.PictureSourceType.PHOTOLIBRARY;
}
else if(sourceSelection==1){
   source=Camera.PictureSourceType.CAMERA;
}
navigator.camera.getPicture(
      function onSuccess(imageData) {
        window.resolveLocalFileSystemURL(imageData, function success(fileEntry) {

          // Do something with the FileEntry object, like write to it, upload it, etc.
          // writeFile(fileEntry, imgUri);
          console.log("got file: " + fileEntry.fullPath);
          // displayFileData(fileEntry.nativeURL, "Native URL");

        }, function (err) {
          console.log(err);
          // If don't get the FileEntry (which may happen when testing
          // on some emulators), copy to a new FileEntry.
          // createNewFileEntry(imgUri);
        });
        $timeout(function () {
          service.imageUrl = "data:image/jpg;base64," + imageData;
          service.imageUploaded = true;
          resolve({
            imgUrl:service.imageUrl
          });
        }, 0);

      }, function onFail(message) {
        reject(false);
      },
      {
        quality: 50,
        destinationType: Camera.DestinationType.DATA_URL,
        // encodingType: Camera.EncodingType.JPEG,
        sourceType: source,
        allowEdit: true,
        correctOrientation:true,
        targetHeight:100,
        targetWidth:200
  }); 

但是当涉及到window.resolveLocalFileSystemURL功能时,它总是失败并且永远不会进入成功回调函数。当我检查错误时说

"A URI supplied to the API was malformed, or the resulting Data URL has exceeded the URL length limitations for Data URLs."

请帮助!!!!

1 个答案:

答案 0 :(得分:0)

从getPicture获取的内容不是文件,而是base64编码的字符串。 然后,您可以按如下方式保存它:

  this.myCamera.getPicture(this.myCameraOptions).then((imageData) => {

    // store in gallery
    this.myBase64.base64ToGallery(imageData, { prefix: '_img' })
    .then( (data) => {
      // success
    }, (dataErr) => {
      // error
    });
  }, (dataErr) => {
      // error
  });