npmPackages:
@react-native-community/cli: ^4.13.0 => 4.13.0
react: 16.13.1 => 16.13.1
react-native: 0.63.3 => 0.63.3
react-native-windows: Not Found
react-native-fs: ^2.16.6
react-native-image-picker: ^2.3.4
IDEs:
Android Studio: Version 3.6.0.0 AI-192.7142.36.36.6241897
Visual Studio: 15.3.26730.12 (Visual Studio Community 2017)
I've been trying to implement a button that opens the camera, takes a picture and saves it to pictures folder.
For this I'm using:
- react-native-image-picker to take the picture
- react-native-fs to move the picture to a filename of my choosing
It's the latter that's giving me a headache. Here's the code I'm using:
<Buttons.Camera containerStyle={{marginBottom:5}}
onPress={() => {
ImagePicker.launchCamera(options, async (response) => {
console.log('Response = ', response);
if (response.didCancel) {
// ToDo cancelled
console.log('Cancellen');
} else if (response.error) {
console.log('Camera Error: ', response.error);
} else {
var RNFS = require('react-native-fs');
const ext = response.fileName?.split('.').pop();
let newPath = `${RNFS.PicturesDirectoryPath}/${options.storageOptions.path}/avatar.${ext}`;
const result = await RNFS.moveFile(response.path, newPath);
this.setState({
avatarPath: newPath,
});
}
});
}} />
A breakpoint is hit on const result = await RNFS.moveFile(response.path, newPath);
. I can confirm that the image does exist in my galary and that newPath
and response.path
are both valid.
When I try to move the file (rename, in fact) I notice that there's no image at newPath
and the original image at response.path
appears to be corrupted and can't be opened anymore.
According to the docs, result
should be true
, but in my case it is undefined
.
The same happens when I use RNFS.exists(newPath)
. The promise resolves but the result is undefined
. No errors are thrown.
Any ideas?