Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native share image

I am trying to share an image, either taken from camera or from gallery, to other devices through various available device's application's(eg whatsApp, skype, email etc.) I found the "Share" function provided but as per my knowledge and research, it only allows to share text data.

Has someone have any idea for sharing an image through a react native application.

Thanks in advance.

like image 464
Rohan Kangale Avatar asked Sep 19 '17 08:09

Rohan Kangale


People also ask

How do I display images in react native app?

Adding Image Let us create a new folder img inside the src folder. We will add our image (myImage. png) inside this folder. We will show images on the home screen.


2 Answers

Solution:

  • Get the path of the image to convert base64.

  • For share image use: react-native-share lib share

  • To access the device directories, I recommend using: rn-fetch-blob. Wikki lib

      dwFile(file_url) {
          let imagePath = null;
          RNFetchBlob.config({
              fileCache: true
          })
          .fetch("GET", file_url)
          // 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);
          });
      }
    

Hope this helps.

like image 133
Bruno Araujo Avatar answered Oct 04 '22 21:10

Bruno Araujo


You can also send the image with Share through the url parameter:

url: "data:image/png;base64,<base64_data>" Cheers

like image 24
Juanan Jimenez Avatar answered Oct 04 '22 20:10

Juanan Jimenez