so I'm trying to display an Image with a dynamic uri/path (at initialization it displays a placeholder image, and then later changes) like so:
<Image source={this.state.pickedImage == null? require('./Placeholder.jpeg'):{uri: this.state.pickedImage}}/>
where this.state.pickedImage
is initialized as null
, and then changed using react-native-image-picker
's launchCamera
and launchImageLibrary
like below:
cameraHandler = () => {
ImagePicker.launchCamera({title: "Pick an Image", maxWidth: 800, maxHeight: 600}, res => {
if (res.didCancel) {
console.log("User cancelled!");
} else if (res.error) {
console.log("Error", res.error);
} else {
this.setState({
pickedImage: res.uri,
imageSize: res.fileSize,
});
}
});
}};
(I should mention that I've implemented launchImageLibrary
in exactly the same way, so I didn't really see a reason to put it here.)
and while the placeholder image shows up just fine, whenever I upload a new image, the <Image/>
doesn't seem to render at all and I'm only left with a background.
What I've tried:
I've tried doing something like initializing this.state.pickedImage
in my state
like:
pickedImage: require('./Placeholder.jpeg').uri
and then adding the image tag like:
<Image source={{uri: this.state.pickedImage}} />
but alas that just ends up preventing me from rendering any images at all.
I've thought about dynamically binding the paths to require()
like here (and also many places on this site), but as I understand it, you need to know what the potential images you might want to be from beforehand, and if I'm using the camera, I can't really do that. (Also, it's not like I can set up a list and export every image on the user's phone from beforehand, so my hands are tied there.) (Apparently, there's no way to really dynamically bind to require() the way I'm looking for, because the package manager doesn't work that way)
Is there any way I can render these uri
s? Any help is appreciated!
(I'm using react-native 0.57.4 btw)