I have to try to unlink to remove image but it storing in the pictures folder,
I have to try to unlink to remove image but it storing in the pictures folder, please can #anyone advice
Originally posted by @musadiq-jinbade in https://github.com/itinance/react-native-fs/issues/352#issuecomment-597551732
me too, @musadiq-jinbade have a solution
try out this on
RNFS.exists(this.state.images)
.then((result) => {
if (result) {
return RNFS.unlink(RNFS.PicturesDirectoryPath)
.then(() => {
console.log('working',RNFS);
return RNFS.unlink(RNFS.ExternalCachesDirectoryPath)
.then(() => {
console.log('error');
return RNFS.unlink(RNFS.PicturesDirectoryPath)
.then(() => {
console.log('error');
})
.catch((err) => {
console.log("error " + err.message);
});
})
.catch((err) => {
console.log("error " + err.message);
}); })
.catch((err) => {
console.log("error photo " + err.message);
});
}
})
.catch((err) => {
console.log(err.message);
});
I have done by this code, check and let me know
In my case, I have my app album inside PicturesDirectoryPath and every time I capture images from camera, It saves the cropped image along with it. I have to delete every file with is not a directory (my app album folder) inside PicturesDirectoryPath after I saved it on my album by this code block
RNFS.readDir(RNFS.PicturesDirectoryPath).then(items => {
items.forEach(item => {
if (item.isFile()) {
RNFS.unlink(
`${RNFS.PicturesDirectoryPath}/${item.name}`
);
}
});
});
I had a similar task - take a photo and send it to server. The photo should NOT be stored in device memory. I used the react-native-community/react-native-image-picker.
RECIPE
- Take a new photo and store it in a separate folder, e.g. images.
- Copy the photo to the cache folder. This photo will be removed when the cache is cleaned.
- Remove the images folder via unlink, it works like in Linux rm -rf
CODE SNIPPET
const options = {
title: 'Take Photo',
cameraType: 'back',
mediaType: 'photo',
quality: 1,
allowsEditing: false,
noData: true,
storageOptions: {
skipBackup: true,
path: 'images',
},
};
const cameraPromise = new Promise((resolve) => {
ImagePicker.launchCamera(options, async (response) => {
const getFileName = response.uri.split('/');
const fileName = getFileName[getFileName.length - 1];
const filePath = response.uri.split('//')[1];
await RNFS.copyFile(
filePath,
`${RNFS.CachesDirectoryPath}/${fileName}`
);
if (Platform.OS === 'ios') {
await RNFS.unlink(`${RNFS.DocumentDirectoryPath}/images`);
} else {
await RNFS.unlink(`${RNFS.PicturesDirectoryPath}/images`);
}
response.uri = `file://${RNFS.CachesDirectoryPath}/${fileName}`;
resolve(response);
});
});