Jim
Jim

Reputation: 2312

How to use Firebase Storage url as react-native Image's source

Im trying to view an image that was stored in Firebase storage, but when I use the download url in <Image />'s source prop, I get a warning:

Warning: Failed prop type: Invalid prop source supplied to Image.

upload function:

const Blob = RNFetchBlob.polyfill.Blob;
const fs = RNFetchBlob.fs;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;

 uploadImage = async (uri, mime = "application/octet-stream") => {
    if (this.state.uploadable == "") {
      alert("No image selected");
    } else {
      const uploadUri =
        Platform.OS === "ios" ? uri.replace("file://", "") : uri;
      const sessionId = new Date().getTime();
      let uploadBlob = null;

      const imageRef = firebase
        .storage()
        .ref("userSpecificFolder")
        .child("profileImage");

      fs.readFile(uploadUri, "base64")
        .then(data => {
          return Blob.build(data, { type: `${mime};BASE64` });
        })
        .then(blob => {
          uploadBlob = blob;
          return imageRef.put(blob, { contentType: mime });
        })
        .then(() => {
          uploadBlob.close();
          return imageRef.getDownloadURL();
        })
        .then(url => {
          this.setState({ storedImg: url });
          resolve(url);
        })
        .catch(err => {
          reject(err);
        });
    }
  };

download function:

downloadImg = () => {
    firebase
      .storage()
      .ref("userSpecificFolder/profileImage")
      .getDownloadURL()
      .then(url => {

        this.setState({ storedImg: url });
      });
  };

Implementation:

<View> 
  <Image source={this.state.storedImg}/>
</View>

If this isn't the way to view an image from Firebase storage what steps are missing?

ps, ive tried the answer posted here with the same outcome mentioned above.

Upvotes: 0

Views: 258

Answers (1)

Jim
Jim

Reputation: 2312

The issue was with the implementation. For some reason the url needs to be the value of the uri property in an object. <Image source={{uri: *url* }} /> and the image displays.

Upvotes: 1

Related Questions