itinance / react-native-fs

Native filesystem access for react-native
MIT License
4.96k stars 984 forks source link

RNFS.readDir sees the file but RNFS.copyFile says the file "doesn't exist". #1085

Open pasllani opened 2 years ago

pasllani commented 2 years ago

I am using React Native Image Crop Picker to select an image from the photo library and attempting to use RNFS.copyFile command to copy this image to a specified directory.

RNFS.readDir sees the file is there and I try to use the result.path from it to copy the file over but it says file doesn't exist. Here is the code I'm using:

         if (image) {
            let imagePath = String(image).split('/');
            let imageName = imagePath.pop();
            let pathString = imagePath.join('/');
            RNFS.readDir(pathString)
            .then((result) => {
                for (let i = 0; i < result.length; i++) {
                    if (result[i].name === imageName) {
                        console.log("Found selected image: " + result[i].path)
                        RNFS.copyFile(result[i].path, vehicleImagesPath + vehicleId + ".jpg")
                        .then(() => {
                            console.log("==> Vehicle image saved");
                        })
                        .catch((error) => {
                            console.log("==> Vehicle image NOT saved: " + error.message);
                        });
                    }
                }
            })
            .catch((error) => {
                console.log("No Dir Found --- " + image)
            });
        }

And here is the output I'm getting: Screen Shot 2022-03-21 at 15 41 15

            Found selected image: /Users/XXXXXXX/Library/Developer/CoreSimulator/Devices/13217381-22BA-459C-84C2-81E7F075D228/data/Containers/Data/Application/4FE78B68-643F-4F75-ADA3-2AF34B0C6FEC/tmp/react-native-image-crop-picker/4304270C-A5E8-4329-B586-A4139CDE8634.jpg
            ==> Vehicle image NOT saved: The file “4304270C-A5E8-4329-B586-A4139CDE8634.jpg” doesn’t exist.

So it's clearly seeing the file but RNFS.copyFile is having an issue finding it.

iamrohitagg commented 2 years ago

@pasllani Were you able to fix this?

pasllani-USX commented 2 years ago

This functionality is surprisingly broken lol. I think it's writing to a directory it doesn't have access to read from. So what I ended up doing is using the includeBase64 property when using ImagePicker.openCamera or ImagePicker.openPicker. Then I took that base64 data from image.data and saved it using RNFS.writeFile("imagePath", image, 'base64'). This method may be a LOT slower since we're storing base64 data directly. Have yet to test it on a large data set. So for now this functionality is STILL BROKEN. This is just a temporary workaround.

iamrohitagg commented 2 years ago

@pasllani-USX Will surely check the workaround you told. Thanks for your response.

ghost commented 2 years ago

same here

cyim02 commented 2 years ago

same here

elisalimli commented 1 year ago

same

Shivani12345 commented 1 year ago

Same issue.Not getting file which is located in application.My image path is 'Users/*******/Documents/reactProjs/CodeStructure/src/assets/icons/icon_collectibles.jpeg if I put statically it works but I want to set it dynamically.Is it possible for iOS?

MMYurt commented 1 week ago

I've been struggling with this problem for a couple of hours. Although the error says the source file doesn't exist, I've realized the problem relates to the destination directory doesn't exist. After I have created the directory with RNFS.mkdir, it's been fixed.