【问题标题】:How to use Firebase Storage url as react-native Image's source如何使用 Firebase 存储 url 作为 react-native Image 的源
【发布时间】:2020-03-17 23:35:37
【问题描述】:

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

警告:失败的道具类型:无效的道具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 发布的答案,结果与上述相同。

【问题讨论】:

    标签: javascript firebase react-native firebase-storage


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2018-01-09
      • 1970-01-01
      • 2018-08-14
      • 2017-09-16
      • 1970-01-01
      • 2021-01-26
      • 2017-01-02
      • 2019-06-22
      • 1970-01-01
      相关资源
      最近更新 更多