将离子图像上传到Firebase存储

时间:2016-08-05 09:05:30

标签: image ionic-framework firebase firebase-storage

I am trying to upload an image from my image gallery into my firebase storage folder with ionic. This works fine for ios emulator but does not work on android. What might be the problem ? What should I check ?

//这是默认选项     $ scope.capturarFoto = function(type){         var opcionesCaptura = {             destinationType:Camera.DestinationType.FILE_URI,             sourceType:Camera.PictureSourceType [type.toUpperCase()],         };

    $cordovaCamera.getPicture(opcionesCaptura)
        .then(procesarImagen, procesarError);
};

function procesarImagen(pathImagen) {
    var directorioFuente = pathImagen.substring(0, pathImagen.lastIndexOf('/') + 1),
        archivoFuente = pathImagen.substring(pathImagen.lastIndexOf('/') + 1, pathImagen.length),
        nombreParaGuardar = new Date().valueOf() + archivoFuente;

    $cordovaFile.readAsArrayBuffer(directorioFuente, archivoFuente)
        .then(function (success) {
            var blob = new Blob([success], {type: 'image/jpeg'});
            enviarFirebase(blob, nombreParaGuardar);
        }, function (error) {
            console.error(error);
        });
}


function enviarFirebase(file, nombre) {
    var storageRef = firebase.storage().ref();
    var uploadTask = storageRef.child('images/' + nombre).put(file);
    uploadTask.on('state_changed', function (snapshot) {
        console.info(snapshot);
    }, function (error) {
        console.error(error);
    }, function () {
        var downloadURL = uploadTask.snapshot.downloadURL;
        console.log(downloadURL);

2 个答案:

答案 0 :(得分:0)

我也是Cordova的新手,但我相信您需要相机功能才能确保您的设备准备就绪:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    console.log(navigator.camera);

    /* Here you can have $scope.choosePhoto = ... */

}

我确定这是您必须采取的方法。试试看,看看它是否有效。

答案 1 :(得分:0)

Ionic 4 + Firestore(保存元信息)+ Firebase存储(存储真实文件)

教程link

enter image description here