0

我正在编程一个应用程序 与离子1和firebase 3.Ionic 1,firebase存储,cordova.getPictures,不保存图像?

我想上传一个图像到firebase的存储。

当我使用html fileupload时,图像被保存,但是当我使用“cordova.getPictures”时,图像不被保存。

代码:

.controller("PhotosCtrl",function($scope,$firebaseArray,$firebaseAuth,$firebaseObject, $timeout, $cordovaImagePicker, $ionicPopup){ 

$scope.upFile = function(){ 

    pickTheImage().then(function(_image) { 
    processImage(_image); 

    }) 

} 

function pickTheImage() { 
     var options = { 
     maximumImagesCount: 1, 
     width: 320, 
     quality: 80 
     }; 

     return $cordovaImagePicker.getPictures(options).then(function (results) { 
      return results[0]; 
     }) 
    } 

    function processImage(_image) { 

    fetch(_image).then(function (_data) { 
     return _data.blob() 
     }).then(function (_blob) { 

     var task=firebase.storage().ref('images/12312355.jpg').put(_blob); 

     task.on('state_changed', 

     function progress(snapshot){ 
     }, 
     function error(err){ 
      $ionicPopup.alert({ 
      title: err 
      }) 
     }, 
     function complete(){ 
      $ionicPopup.alert({ 
       title: "Imagen guardada!" 
      }) 
     } 
    ); 
     }) 
    } 
}) 

回答

0

从相机捕获图像将返回IMAGEURL。如果你想转换为blob类型,那么在processImage函数中使用下面的代码。

//Read the file entry from the file URL 
      resolveLocalFileSystemURL(imgUrl, function (fileEntry) { 
       fileEntry.file(function (file) { 
        var reader = new FileReader(); 
        reader.onloadend = function() { 
         var imgBlob = new Blob([this.result], { 
           type: file.type 
          }); 
         var imgFile = imgBlob; 

        }; 
        reader.readAsArrayBuffer(file); 
       }); 
      }, function() { 
       //on Error 
      }); 

您也可以参考 http://ourcodeworld.com/articles/read/22/solve-native-path-of-android-content-if-resolvelocalfilesystemurl-doesn-t-work