2017-07-04 32 views
0

我想使用cordova camera插件獲取上傳文件的類型和大小。我使用下面的代碼如何使用cordova camera插件獲取上傳文件的文件大小和文件類型

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." 

請幫助!!!!

回答

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 
    }); 
相關問題