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.

Ismeet
  • 249
  • 2
  • 11

1 Answers1

-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);
    });
}
Aurangzaib Rana
  • 2,353
  • 10
  • 16