Vladimir
Vladimir

Reputation: 412

How can I get the absolute image path in react-native?

I need to create a video based on audio and an image. I saved an image in my app folder /src/img/test.jpg. To create a video, I need an absolute image path.

I tried react-native-fs. Here is some code, where I'm trying to copy an image from src path to DocumentDirectoryPath.

const dest = `${RNFS.DocumentDirectoryPath}/test.jpg`;
const path =  'src/img/test.jpg'

RNFS.copyFile(path, dest)
  .then(() => FileViewer.open(dest))
    .then(() => {
      RNFS.readDir(RNFS.DocumentDirectoryPath) 
      .then((result) => {
        console.log('GOT RESULT', result);
      })
    })
  .catch(_err => {
    console.log(_err);
});

And I'm getting the error: Error: The file “test.jpg” couldn’t be opened because there is no such file.

Upvotes: 2

Views: 2685

Answers (1)

Maytham
Maytham

Reputation: 151

you can't access

const path =  'src/img/test.jpg'

in run time because it's bundled within your app

try this :

const dest = `${RNFS.DocumentDirectoryPath}/test.jpg`;
const img =  require('src/img/test.jpg');

RNFS.writeFile(dest, img, "base64")
  .then(() => FileViewer.open(dest))
  .then(() => {
     RNFS.readDir(RNFS.DocumentDirectoryPath) 
  .then((result) => {
    console.log('GOT RESULT', result);
     })
  })
  .catch(_err => {
     console.log(_err);
  });

Upvotes: 4

Related Questions