Ionic 1, firebase storage, cordova.getPictures, not save images?

292 Views Asked by At

I'm programming an application with ionic 1 and firebase 3.

I want to upload an image to the storage of firebase.

When I use html fileupload the image is saved but when i use "cordova.getPictures" the image is not saved.

The code:

.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!"
            })
        }
    );
      })
  }
})
1

There are 1 best solutions below

0
On

Capturing image from camera will return the imageUrl. If you want to convert to blob type then using the below code in processImage function.

//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
            });

You can also refer http://ourcodeworld.com/articles/read/22/solve-native-path-of-android-content-if-resolvelocalfilesystemurl-doesn-t-work