I am trying to allow the user to upload an image to Firebase Real-time database. I have used both 'rn-fetch-blob' and 'react-native-fetch-blob', but couldn't get it to work.
The photo is selected through this function which saves it to the avatar state.
selectPhoto() {
const options = {
quality: 1.0,
maxWidth: 500,
maxHeight: 500,
storageOptions: {
skipBackup: true
}
};
ImagePicker.showImagePicker(options, (response) => {
if (response.didCancel) {
console.log('User cancelled photo picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
let source = { uri: response.uri };
this.setState({ avatar: source });
}
})
}
The uploadPhoto then tries to upload it to Firebase but I am getting a "missing argument 'path'" error on the line 'RNFetchBlob.fs.readFile'
uploadPhoto(mime = 'application/octet-stream') {
return new Promise((resolve, reject) => {
const uploadUri = this.state.avatar;
let uploadBlob = null
console.log('uid' + this.state.uid);
const imageRef = firebase.storage().ref('applicants' + this.state.uid).child('profile_pic');
console.log(this.state.avatar);
RNFetchBlob.fs.readFile(this.state.avatar, 'base64')
.then((data) => {
return Blob.build(data, { type: `${mime};BASE64` })
})
.then((blob) => {
uploadBlob = blob
return imageRef.put(blob_ref, blob, { contentType: mime })
})
.then(() => {
uploadBlob.close()
return imageRef.getDownloadURL()
})
.then((url) => {
resolve(url)
})
.catch((error) => {
alert(error.message);
reject(error);
})
})
}
this.state.avatarat the time of the call? - Doug Stevenson