如何使用Firebase存储url作为本机图像的源

时间:2019-11-21 23:03:20

标签: javascript firebase react-native firebase-storage

我正在尝试查看存储在Firebase存储器中的图像,但是当我使用<Image />的源道具中的下载网址时,会收到警告:

  

警告:道具类型失败:提供给source的道具Image无效。

上传功能:

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);
        });
    }
  };

下载功能:

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

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

实施:

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

如果这不是从Firebase存储中查看图像的方法,缺少哪些步骤?

ps,香港专业教育学院尝试了here发布的答案,其结果与上述相同。

1 个答案:

答案 0 :(得分:1)

问题在于实施。由于某种原因,URL需要是对象中uri属性的值。 <Image source={{uri: *url* }} />并显示图像。