简体   繁体   English

如何使用 Firebase 存储 url 作为 react-native 图片来源

[英]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:我试图查看存储在 Firebase 存储中的图像,但是当我在<Image />的源道具中使用下载 url 时,我收到警告:

Warning: Failed prop type: Invalid prop source supplied to Image .警告:道具类型失败:提供给Image的道具source无效。

upload function:上传 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:下载 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?如果这不是从 Firebase 存储查看图像的方法,那么缺少哪些步骤?

ps, ive tried the answer posted here with the same outcome mentioned above. ps,我尝试了此处发布的答案,结果与上述相同。

The issue was with the implementation.问题在于实施。 For some reason the url needs to be the value of the uri property in an object.由于某种原因,url 需要是 object 中 uri 属性的值。 <Image source={{uri: *url* }} /> and the image displays. <Image source={{uri: *url* }} />并显示图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM