1

I'm facing an error in my ionic android app. Using the exact same code as my ios app (ionic with two platforms : android and ios), i'm not able to send a base64 uri to Firebase storage.

The error is :

Uncaught TypeError: mountainImagesRef.putString is not a function at navigator.camera.getPicture.quality (controllers.js:202) at Object.callbackFromNative (cordova.js:294) at <anonymous>:1:9

I saw something similar here, but the solution there was to update the plugin, and I'm already using the last version of cordova-plugin-camera : V 4.0.3. Uncaught TypeError: ref.putString is not a function

On my ios app, .putString() function is working ! How can I make this work on my android app ? Do you see something I did wrong ? I've tried many ways to use putString like uri.substring(0,23)...etc.

 function takePic()
 {
 
  navigator.camera.getPicture(
    function(uri){

 $ionicPopup.alert({
          title: 'A new image',
          template: 'Loading !'
 });
 
 // Create a root reference
 var storageRef = firebase.storage().ref();

 var filename = Math.floor((Math.random()*10000)+ 1);
 var newAvatar_name = filename+".jpg";

 var mountainImagesRef = storageRef.child("avatars/"+newAvatar_name);

 mountainImagesRef.putString(uri, "base64").then(function(snapshot) {
  console.log("Uploaded a base64 string!");
 });

    },
    function(){
    $ionicPopup.alert({
          title: 'Error',
          template: 'impossible to select one'
          });
    },
    {

         quality: 60,
         targetHeight: 300,
         targetWidth: 300,
         destinationType: 0,
         sourceType: 1,
         mediaType: 0,
         saveToPhotoAlbum: false
     }
     );
}
Memphis
  • 410
  • 5
  • 23
  • check my answer here=> https://stackoverflow.com/a/51471159/1081909 – NullPointer Aug 01 '18 at 14:54
  • Thank you for your reply, but I already tried to change the way I'm using `putString()`, but the problem seems to be `putString()` is not known... Also, with your method i'm getting this error : `window.resolveLocalFileSystemURL is not a function` – Memphis Aug 01 '18 at 15:35

1 Answers1

0

I finally came to a solution, instead of using putString() which seems to be the problem, I converted the camera uri to a blob file and then use put().

function takePic()
 {
 
  navigator.camera.getPicture(
    function(uri){

 $ionicPopup.alert({
          title: 'A new image',
          template: 'Loading !'
 });
 
 // Create a root reference
 var storageRef = firebase.storage().ref();

 var filename = Math.floor((Math.random()*10000)+ 1);
 var newAvatar_name = filename+".jpg";

 var mountainImagesRef = storageRef.child("avatars/"+newAvatar_name);

// Convert the uri into a byteArray

var contentType = contentType || '';
    var sliceSize = sliceSize || 512;

    let byteCharacters = atob(uri);
    let byteArrays = [];

    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        let slice = byteCharacters.slice(offset, offset + sliceSize);

        let byteNumbers = new Array(slice.length);
        for (let i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }

        let byteArray = new Uint8Array(byteNumbers);

        byteArrays.push(byteArray);
    }


    var mountainImagesRef = storageRef.child("avatars/"+newAvatar);

// Use the byteArrays to get my Blob

    var blob = new Blob(byteArrays);

    mountainImagesRef.put(blob).then(function(snapshot) {
        console.log("Uploaded a base64 string!");
    });
    
  },
    function(){
    $ionicPopup.alert({
          title: 'Error',
          template: 'impossible to select one'
          });
    },
    {

         quality: 60,
         targetHeight: 300,
         targetWidth: 300,
         destinationType: 0,
         sourceType: 1,
         mediaType: 0,
         saveToPhotoAlbum: false
     }
     );
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Memphis
  • 410
  • 5
  • 23