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!!