3

In my ReactNative application I am using "react-native-vision-camera": "^2.13.5" for taking pictures.

After the picture is taken and uploaded, I want to delete it from the device, but the RNFS (react-native-fs) cannot find the file by the given path on iOS and Android devicess:

I get the error

iOS ; [Error: ENOENT: no such file or directory, open '/private/var/mobile/Containers/Data/Application/1BB2256A-ED83-407D-9C76-07431268779B/tmp/ReactNative/CE2FA429-76C8-4BD9-925A-BED09B0B77BB.jpeg'] Android ; [Error: File does not exist]

The path in Android is like: /data/user/0/packagename/cache/mrousavy2423256141715663083.jpg

const photo = await camera.current.takePhoto(); // camera: React.RefObject<Camera> 
const task = storage().ref(<ref>).putFile(photo.path) 
const result = await task.then(); // Firebase Storage can read and upload the photo


RNFS.stat(photo.path)  // <-- Error: The file “CE2FA429-76C8-4BD9-925A-BED09B0B77BB.jpeg” couldn’t be opened because there is no such file.

RNFS.unlink(photo.path) // <-- [Error: ENOENT: no such file or directory, open '/private/var/mobile/Containers/Data/Application/1BB2256A-ED83-407D-9C76-07431268779B/tmp/ReactNative/CE2FA429-76C8-4BD9-925A-BED09B0B77BB.jpeg']

// Prepending file://  Does not work too
RNFS.unlink(`file://${photo.path}`)
RNFS.stat(`file://${photo.path}`) 

Is it because of access permission that react-native-fs needs?

How does the firebase storage have access to the photo?

Is the path a real temp folder that gets cleared after the app is closed?

why react-native-vision-camera saves media there by default?

Do I really need to remove that media manually or does it get removed?

env:
    "react-native": "0.68.2",
    "react-native-fs": "^2.20.0",
    "react-native-vision-camera": "^2.13.5",
    physical device : iPhone 8 Plus with iOS 15.5
Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123

2 Answers2

0

There is no need to delete the file as the photos are being stored in a temporary directory. It will be deleted when the app closes.

https://mrousavy.com/react-native-vision-camera/docs/api/interfaces/PhotoFile

caslawter
  • 631
  • 1
  • 6
  • 11
  • It does not look like that, at least in Android. I cleared the cache, took some photos, and the cache reached ~10MB. Closing/killing the app did not reduce the cache size. – Amir-Mousavi Jul 05 '22 at 14:37
  • Weird, in that case did you set the read and write files permission in the android manifest? – caslawter Jul 05 '22 at 15:06
  • I just tried deleting the image on my side and it works, can you add an console.log(await RNFS.stat(photo.path)); and see if it logs anything? – caslawter Jul 05 '22 at 16:10
-1

Instead of calling RNFS.unlink(photo.path) try RNFS.unlink(photo.uri). The react-native-vision-camera API outputs a File object with a URI not a path.

  • This is a wrong answer. Taking a picture returns a `PhotoFile` object. And if you check the Documentation for [API / Interface: PhotoFile](https://mrousavy.com/react-native-vision-camera/docs/api/interfaces/PhotoFile) There is NO `uri` attribute, but only `path` is defined. – Amir-Mousavi Jul 06 '22 at 15:37