【问题标题】:React Native don't show image when I fetch image file path from database当我从数据库中获取图像文件路径时,React Native 不显示图像
【发布时间】:2021-09-29 16:42:47
【问题描述】:

我有一个包含课程表的数据库。

我想通过字段图片栏中保存的图片路径来展示图片。

在我的 react native 应用程序中,我有一个组件列出了购物车,其中购物车的图像保存在磁盘中

喜欢:C:\Users\Public\Pictures\Rectangle_2.png。然后我把路径放在图片列中。

CartList.js

import Carts from './../Cart';
const Item = ({item}) => {
  return (
    <Carts
      name={item.name}
      price={item.price}
      offPrice={item.offPrice}
      image={item.image}
    />
  );
};
class CartList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
    };
  }
  componentDidMount() {
    this.requestAPI();
  }
  requestAPI = () => {
    const apiURL = 'http://myApi/api/courses';
    fetch(apiURL)
      .then(response => response.json())
      .then(responseJson => {
        this.setState({
          data: responseJson,
        });
      });
  };

  renderItem = ({item}) => {
    return <Item item={item} />;
  };
  render() {
    return (
      <>
        <FlatList
          data={this.state.data}
          renderItem={this.renderItem}
          keyEtractor={item => item.id.toString()}
        />
      </>
    );
  }
}

我通过购物车中的道具访问图片路径。

Cart.js

const Cart = (props) => {
  return (
    <View
      <Image
        style={{
          width: '40%',
          height: 100,
        }}
        source={{uri:props.image}}
      />
    </View>
  );
};
export default Cart;

当我运行应用程序时,我收到此错误:

我该如何解决这个问题?

【问题讨论】:

  • 你试过记录你的 src 吗?每个图像都应该有文档中提到的宽度和高度。
  • 好吧,首先,您的文件指向 C 驱动器上的文件。您的手机拥有自己的独立文件系统,因此无法访问服务器 C 盘上的文件。你可以做的是让你的端点返回图像的字节而不是图像的路径
  • 我没有尝试记录我的 src。每个图像都有宽度和高度,但它不起作用。

标签: javascript reactjs database react-native


【解决方案1】:

如果您的图片路径不正确,会出现这种情况,请检查您的图片远程路径是否正确,我认为图片路径具有误导性。

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2013-02-22
    • 1970-01-01
    • 2014-04-16
    相关资源
    最近更新 更多