3 thoughts on “is there a way to share image as well as url from this library

  1. I came up with this workaround.
    First parse the library url into assets url

    const parseUri = (uri) => {
      const id = uri.split('photos://')[1].split('/L0/001')[0];
      return `assets-library://asset/asset.JPG?id=${id}&ext=JPG`
    }
    

    And then use RNFetchBlob from react-native-fetch-blob to read from assets url and convert as base64 string

    As we got the base64 string we can share it : >

    RNFetchBlob.fs
          .readFile(
            parseUri(libraryUrl),
            "base64"
          )
          .then(base64data => {
            let base64Image = `data:image/jpeg;base64,${base64data}`;
            Share.open(
              {
                url: base64Image
              }
            )
              .then(() => console.log("He shared!!"))
              .catch(() => console.log("He didn't share :("));
          });
    
  2. Hey, thanks for the support after more testing I found the problem only happens on iOS, on android you are able to (more or less) easily share and image and a message, but on iOS you cannot place both of them, here is the final code I ended up:

    try {
    const sharingMessage = 'Lassen Sie uns unsere Kommunikation vereinfachen'
          if (Platform.OS === 'ios') {
            url = url.slice(7);
          }
    
          // https://github.com/EstebanFuentealba/react-native-share/issues/125
          // base64 fallback for Android
          let data = '';
          RNFetchBlob.fs.readStream(url, 'base64', 4095).then(ifstream => {
            ifstream.open();
            ifstream.onData(chunk => {
              data += chunk;
            });
            ifstream.onError(err => {
              console.warn('oops', err);
            });
            ifstream.onEnd(() => {
              url = 'data:image/jpeg;base64,' + data;
    
              Share.open({
                message: Platform.OS === 'android' ? sharingMessage : undefined, // if this field is set on iOS, sharing the image does not work
                url: url,
                mime: 'image/jpeg',
                subject: sharingMessage,
              }).catch((err: any) => null);
            });
          });
        } catch (e) {
          console.warn('error', e);
        }
    
  3. I put the comment snippet a few comments up there ^, did not produce a repository, it should be a matter of copying and pasting

1 possible answer(s) on “is there a way to share image as well as url from this library

  1. I came up with this workaround.
    First parse the library url into assets url

    const parseUri = (uri) => {
      const id = uri.split('photos://')[1].split('/L0/001')[0];
      return `assets-library://asset/asset.JPG?id=${id}&ext=JPG`
    }
    

    And then use RNFetchBlob from react-native-fetch-blob to read from assets url and convert as base64 string

    As we got the base64 string we can share it : >

    RNFetchBlob.fs
          .readFile(
            parseUri(libraryUrl),
            "base64"
          )
          .then(base64data => {
            let base64Image = `data:image/jpeg;base64,${base64data}`;
            Share.open(
              {
                url: base64Image
              }
            )
              .then(() => console.log("He shared!!"))
              .catch(() => console.log("He didn't share :("));
          });