显示存储在手机上的图像

时间:2016-12-01 12:21:36

标签: react-native

我已经成功地在我的应用程序中拍摄照片并将其存储在手机上,但我该如何显示它?

我尝试了这种静态方法,但它不起作用(是的,图像在那里:))

 <Image width={40} height={40} source={{uri: '/storage/emulated/0/DCIM/IMG_20161201_125218.jpg'}} />

3 个答案:

答案 0 :(得分:9)

即使我遇到了这个问题并得到了解决。你必须在file://前面添加文件路径(注意双/)。

<Image source{{uri:'file:///storage/emulated/0/DCIM/IMG_20161201_125218.jpg'}} style={myImageStyle}/>

这里是reference

我希望这会有所帮助:)

答案 1 :(得分:1)


我们应该获得“ READ_EXTERNAL_STORAGE”权限


  • 导入

// import 
import { PermissionsAndroid } from "react-native";
  • 请求权限的功能

// function to request permission
requestStoragePermission = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
        {
          title: "Permission title",
          message:
            "Permission message",
          buttonNeutral: "Ask Me Later",
          buttonNegative: "Cancel",
          buttonPositive: "OK",
        }
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log("You can use the EXTERNAL_STORAGE");
      } else {
        console.log("EXTERNAL_STORAGE permission denied");
      }
    } catch (err) {
      console.warn(err);
    }
  };
  • 调用上述函数

<Button title="request permissions" onPress={this.requestStoragePermission} />

reactnative android permissions docs单击此

答案 2 :(得分:0)

    <Image source = {{uri: "file:///storage/emulated/0/appiwell/image/qkpOyUaRla.jpg", 
                    width: responsiveWidth(100), 
                    height: responsiveHeight(100)}}/>

注意,应在{uri,width,height}内设置宽度,高度。宽度,高度的风格可能不起作用。

responsiveWidth,responsiveHeight here https://www.npmjs.com/package/react-native-responsive-dimensions