1
votes
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 ?

// this is the default options $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
did you find a solution. It's 2018 and I can't get it work for android, only ios. - benevolentprof

2 Answers

0
votes

I am also new to Cordova but I believe for Camera functionality you would need to ensure that your device is ready:

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

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

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

}

I'm sure this is the approach you'd have to take. Try that and see if it works.

0
votes

Ionic 4 + Firestore(Save Meta info) + Firebase Storage(Store real files)

Tutorial link

enter image description here