0

i need share local image on react native app, use react-native share and react-native-fs. Image is in local folder in root app named 'images'. How to share this image. Do i need copy or move image to temp and use that image for getting absolute path.

This is my code. rnfs.movefile don't work

getAssetFileAbsolutePath = async () => {
  const dest =
  `${RNFS.TemporaryDirectoryPath}${Math.random().toString(36)
   .substring( 
    .7)}.png`;
  const img = './images/page1.png';
  try {
    await RNFS.moveFile(img, dest);
    console.log("dobro", dest)
  } catch(err) {
    console.log("greska", err)
  } 
 }

I get error “page1.png” couldn’t be moved to “tmp” because either the former doesn’t exist, or the folder containing the latter doesn’t exist.

1 Answer 1

-1

use rn-fetch blob with react native share first find path of the image and convert it to base64. then share image use react native share package

ShareFile(file) {
    let imagePath = null;
    RNFetchBlob.config({
        fileCache: true
    })
    .fetch("GET", file)
    // the image is now dowloaded to device's storage
    .then(resp => {
        // the image path you can use it directly with Image component
        imagePath = resp.path();
        return resp.readFile("base64");
    })
    .then(async base64Data => {
        var base64Data = `data:image/png;base64,` + base64Data;
        // here's base64 encoded image
        await Share.open({ url: base64Data });
        // remove the file from storage
        return fs.unlink(imagePath);
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

is file string like './images/page1.png'?
have error Possible Unhandled Promise Rejection (id: 2): Error: unsupported URL
in case .fetch("GET", file) file must be API?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.