0

我正在使用 ionic 1 和 firebase 3 编写应用程序。

我想将图像上传到 Firebase 的存储中。

当我使用 html 文件上传时,图像被保存,但是当我使用“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!"
            })
        }
    );
      })
  }
})
4

1 回答 1

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

于 2016-10-13T15:51:41.867 回答