I'm trying to save an image to document directory (instead of cache) using react native and react-native-fs library, but instead I get a blank photo.
const localPath = (image) => {
console.log("Cache directory path is ", image)
const fileName = image.split('/').pop();
console.log('Filename is ',fileName)
const newPath = 'file://' + RNFS.DocumentDirectoryPath + '/' + fileName;
console.log(newPath)
// write the file
RNFS.writeFile(newPath, image, 'base64')
.then((success) => {
console.log('IMG WRITTEN!');
console.log(newPath)
})
.catch((err) => {
console.log(err.message);
});
return newPath
}
So I want to use this newPath
<Image style={styles.image} source={{ uri: newPath }} />
but a blank photo appears..... If I pass the original path, then the photo is displayed.
If image
is a path to a file just use the copyFile method
const localPath = (image) => {
console.log('Cache directory path is ', image)
const fileName = image.split('/').pop();
console.log('Filename is ', fileName)
const newPath = `${RNFS.DocumentDirectoryPath}/${fileName}`; // You don't really need the `'file://` prefix
console.log(newPath);
// COPY the file
RNFS.copyFile(image, newPath)
.then((success) => {
console.log('IMG COPIED!');
console.log(newPath);
})
.catch((err) => {
console.log(err.message);
});
return newPath
}
There's also a moveFile that you can use to move the file instead of copying
writeFile
is used to write content to a file
The function you use to copy the image and return the newPath
is not async - it would return the newPath
immediately - possibly before the image is copied to the document directory - this might cause a problem if the <Image>
tries to load it before the file is actually there.
I would only return the newPath
after the image is actually copied
const localPath = async (image) => {
const newPath = `${RNFS.DocumentDirectoryPath}/${fileName}`;
await RNFS.copyFile(image, newPath);
return newPath;
}
// usage
const uri = await localPath(image);