React Native not loading image from sdcard in android

后端 未结 3 1564
生来不讨喜
生来不讨喜 2021-01-04 18:26

I am trying to load image from external sdcard after I take photo from camera in React native image component as following, but it is not rendering in android. I even check

相关标签:
3条回答
  • 2021-01-04 18:44

    We should get 'READ_EXTERNAL_STORAGE' Permission

    refer this

    0 讨论(0)
  • 2021-01-04 18:55

    Just had this issue, solved it by prepending the filepaths with file://

    So, instead of:

     <Image source={{uri:'/storage/emulated/0/Pictures/myImage.jpg'}} style={myImageStyle} />
    

    Use:

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

    Note the 3 consecutive slashes as opposed to just two as suggested by @Cherniv.

    0 讨论(0)
  • 2021-01-04 19:02

    DSquare is right, unless you're running IOS. In that case 'file://' doesn't work.

    <Image source={{uri:`${Platform.OS === "android" ? 'file://' : ''}/storage/emulated/0/Pictures/myImage.jpg`}} style={myImageStyle} />
    

    The second problem is that this the path /storage/emulated/0 does not exist on IOS either.

    You can make your life easier by using react-native-fs

    import RNFS from "react-native-fs";
    ...
    renderPhoto() {
        return <Image source={{uri:`${Platform.OS === "android" ? 'file://' : ''}/${RNFS.PicturesDirectoryPath}/myImage.jpg`}} style={myImageStyle} />
    }
    

    RNFS.PicturesDirectoryPath translates to /storage/emulated/0/Pictures on Android.

    No more headaches, no more wondering what path is which on which device/platform.

    0 讨论(0)
提交回复
热议问题