2

I've been having trouble viewing the image files I've uploaded to firebase and just noticed the issue is with the file type in firebase.

enter image description here

Two files in my firebase storage console. One uploaded from my IOS simulator (octet-stream) and the other uploaded directly into the console from the browser which uploads properly and is viewable.

Here are my select and upload functions:

_selectPhoto = async () => {

    const status = await getPermission(Permissions.CAMERA_ROLL);
    if (status) {
      let imageName = "pic"
      const result = await ImagePicker.launchImageLibraryAsync(options);
      if (!result.cancelled) {
        Animated.timing(this.animatedWidth, {
          toValue: 600,
          duration: 15000
      }).start()
        this.uploadImage(result.uri, imageName)
        .then(() => {
        this.props.navigation.navigate('Profile')
        })        
        .catch((error) => {
          Alert.alert('Must Sign In');
          this.props.navigation.navigate('Login')
          console.log(error);

        })
      }
    }


  };


uploadImage = async (uri, imageName) => {

    const user = firebase.auth().currentUser;
    const response = await fetch(uri);
    const blob = await response.blob();
    let storageRef = firebase.storage().ref().child(''images/'+user.displayName+'/'+imageName+'.jpg'');

    const snapshot = await storageRef.put(blob);
    blob.close();
    snapshot.ref.getDownloadURL().then(function(downloadURL) {
      console.log("File available at", downloadURL);
      user.updateProfile({
        photoURL: downloadURL.toString(),
        }).then(function() {
            console.log('saved photo')

        }).catch(function(error) {
            console.log('failed photo')

        });

    });

}

When I get the link in my console, it also has the media&token:

... .appspot.com/o/profile-pic.jpg?alt=media&token=56eb9c36-b5cd-4dbb-bec1-3ea5c3a74bdd

If I CMD+Click in VS Code I receive an error:

{
  error: {
    code: 400,
    message: "Invalid HTTP method/URL pair."
  }
}

So naturally, when I put that link in the browser it downloads a file with that name but says:

The file “pic.jpg” could not be opened. It may be damaged or use a file format that Preview doesn’t recognize.

Maybe it could be something with mediaTypes, but I'm not exactly sure how to use it.

mediaTypes : String -- Choose what type of media to pick. Usage: ImagePicker.MediaTypeOptions., where is one of: Images, Videos, All.

Thanks!

erica
  • 21
  • 5

1 Answers1

4

I've been fighting with this same issue for the past few days. I was finally able get images to upload and render as expected by following the Firebase Upload example in the Expo repo. I don't fully understand why it works, but it seems like Firebase doesn't like the blob that's generated by

const blob = await response.blob();

Try replacing the above with:

const blob = await new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.onload = function() {
      resolve(xhr.response);
    };
    xhr.onerror = function(e) {
      console.log(e);
      reject(new TypeError('Network request failed'));
    };
    xhr.responseType = 'blob';
    xhr.open('GET', uri, true);
    xhr.send(null);
  });