1
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);
burc
  • 41
  • 5

2 Answers2

0

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.

Zain
  • 21
  • 2
  • 6
  • Thank you Zain, I found my answer here : http://stackoverflow.com/questions/37452489/uploading-image-to-firebase-storage-from-cordova-app – burc Aug 20 '16 at 04:39
  • This example that I sent to you as link, works only with ios but does not work with android. What might be the problem ? – burc Aug 21 '16 at 16:31
0

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

Tutorial link

enter image description here

Code Spy
  • 9,626
  • 4
  • 66
  • 46