Cordova.file returns null while Uploading image to firebase

I am trying to upload an image to firebase storage using cordova.ImagePicker. I get the file_URI but I can’t read the the file asArrayBuffer. The cordova file plugin is not working. Here is my code:

ionic.Platform.ready(function(){
$cordovaImagePicker.getPictures(options)
.then(function (results) {
console.log('Image URI: ’ + results[0]);
$scope.profilePicture = results[0];
alert(results[0]);
// lets read the image into an array buffer…
// see documentation:
// http://ngcordova.com/docs/plugins/file/
fileName = results[0].replace(/^.*[\/]/, ‘’);
alert(fileName);
// modify the image path when on Android
if ($ionicPlatform.is(“android”)) {
path = cordova.file.cacheDirectory
} else {
path = cordova.file.tempDirectory
}
alert(JSON.stringify(cordova.file.cacheDirectory)); //returns null
var filePath = cordova.file.cacheDirectory +"/" +fileName;
alert(filePath);
$cordovaFile.readAsArrayBuffer(path, fileName)
.then(function(succcess){
alert(“success”)
var imageBlob = new Blob([success], { type: “image/jpeg” });
var storage = userAuth.getStorage();
var saveRef = storage.ref(‘images’).child(fileName);
var uploadTask = saveRef.put(imageBlob);
uploadTask.on(‘state_changed’, function(snapshot){

         }, function(error){
           alert("Database error: " + error);
         }, function(){
           var downloadURL = uploadTask.snapshot.downloadURL;
           alert(downloadURL);
         });

    }, function (error) {
       // error
       alert("Image Picker error: " + error)
    });

});
};

I can’t upload the image to firebase. Please help!!

same problem with me :frowning:

I fixed the problem. The problem was that the code doesn’t run on ionic view. Cordova File is not supported in the view. You have to run on an actual device.

Hi fu05bu.

cordova.file still not working on Ionic View?

I’m getting null on cordova.file.dataDirectory on View however on android emulator works fine.

Thanks

Yes you will get a null on the ionic view because the cordova.file is not one of the supported plugins on the ionic view. It will work with emulator or an actual device. I would recommend running your testing on an actual device as it shows you errors that you don’t see on the ionic view.