1 thought on “Is it possible to share multiple images at once?

  1. @Kampeone I had to use RNFS (react-native-fs) and:
    1- Download the image file to the device;
    2- Read the downloaded file;
    3- Pass the file contents as a base64 string to react-native-share.

    import RNFS from 'react-native-fs';
    import Share from 'react-native-share';
    
    share(file, fileName) {
      const localFile = `${RNFS.DocumentDirectoryPath}/${fileName}`;
      const options = {
        fromUrl: file,
        toFile: localFile,
      };
      RNFS.downloadFile(options).promise
        .then(() => this.share1(localFile))
        .catch(this.onError);
    }
    share1(localFile) {
      RNFS.readFile(localFile, 'base64')
        .then(base64 => {
          const mimeType = Utils.getMimeType(localFile);
          Share.open({
            type: mimeType,
            url: `data:${mimeType};base64,${base64}`,
          });
        });
    }
    

    Images could be JPG or PNG, so I had a Utils.getMimeType method to get the correct MIME type. It just read the file extension and returned the proper MIME type string (image/jpeg or image/png) accordingly.

Comments are closed.

2 thoughts on “Is it possible to share multiple images at once?

  1. @Kampeone I had to use RNFS (react-native-fs) and:
    1- Download the image file to the device;
    2- Read the downloaded file;
    3- Pass the file contents as a base64 string to react-native-share.

    import RNFS from 'react-native-fs';
    import Share from 'react-native-share';
    
    share(file, fileName) {
      const localFile = `${RNFS.DocumentDirectoryPath}/${fileName}`;
      const options = {
        fromUrl: file,
        toFile: localFile,
      };
      RNFS.downloadFile(options).promise
        .then(() => this.share1(localFile))
        .catch(this.onError);
    }
    share1(localFile) {
      RNFS.readFile(localFile, 'base64')
        .then(base64 => {
          const mimeType = Utils.getMimeType(localFile);
          Share.open({
            type: mimeType,
            url: `data:${mimeType};base64,${base64}`,
          });
        });
    }
    

    Images could be JPG or PNG, so I had a Utils.getMimeType method to get the correct MIME type. It just read the file extension and returned the proper MIME type string (image/jpeg or image/png) accordingly.

Comments are closed.