javascriptfirebasereact-nativefirebase-storagereact-native-image-picker

Images put to storage are saved as 'octet-stream' rather than image/jpeg (firebase and ReactNative)


I am using the camera(react-native-image-Picker) to take a pick and save it to storage. Here is how I am doing it.

const saveImage = async () => {
    const id = firebase.firestore().collection('food').doc().id
    const storageRef = firebase.storage().ref()
    const fileRef = storageRef.child(file.fileName) //name of image to store
    await fileRef.put(file) //store image

    firebase.firestore().collection("food").doc(id).update({
      image: firebase.firestore.FieldValue.arrayUnion({
        name: file.fileName,
        url: await fileRef.getDownloadURL()
      })
    })
}

console.log(typeof file);
gives => "object"

console.log(file);
//gives => 
file = {height: 2322, 
uri:"content://com.photodocumentation.imagepickerprovidlib_temp_7a0448df-1fac-4ac7-a47c-402c62ecce4c.jpg", 
width: 4128, 
fileName: "rn_image_picker_lib_temp_7a0448df-1fac-4ac7-a47c-402c62ecce4c.jpg", 
type: "image/jpeg"}

Results: In Firebase (storage) The image is being saved as application/octet-stream instead of image/jpeg. The image is not shown, it says undefined when downloaded from storage.

Any help will be so appreciated.


Solution

  • This is how I was able to fix it:

    const uploadImage = async () => {
        const response = await fetch(file.uri)
        const blob = await response.blob();
        var ref = firebase.storage().ref().child("FolderName");
        return ref.put(blob)
    }