Share a screenshot and a text message (or a link), using react-native and expo

1.1k Views Asked by At

I'm trying to have a share button, that would take a screenshot of the current place in the app, and share it to social platforms along with some text or a link.

I've tried using expo-sharing, react-native-view-shot, and expo-file-system, and another try using the core react-native share and react-native-view-shot, and I either succeed on sharing a screenshot, or sharing a text, but not both. I've looked around a lot and still didn't have success with it.

Here are the most successful 2 tries:

First code with expo-sharing, expo-file-system, and react-native-view-shot, shares the screenshot but not the text message:

const viewShot = React.useRef();
const onShare = async () => {
  try {
    const uri = await viewShot.current.capture();
    const message = 'Your message here';

    // create a temporary file that contains the image and the message
   const tempFilePath = `${FileSystem.cacheDirectory}TempImage.jpg`;
    await FileSystem.writeAsStringAsync(tempFilePath, message, {
      encoding: FileSystem.EncodingType.UTF8,
    });
    await FileSystem.copyAsync({
      from: uri,
      to: tempFilePath,
    });
 
    // share the temporary file using the `Sharing.shareAsync` method
    //await Sharing.shareAsync(`file://${tempFilePath}`, { message });
    await Sharing.shareAsync(uri, { message });
  } catch (error) {
    console.error('Oops, snapshot failed', error);
  }
};

or

const viewShot = React.useRef();
const onShare = async () => {
  try {
    const uri = await viewShot.current.capture();
    const message = 'Your message here';

    //await Sharing.shareAsync(`file://${tempFilePath}`, { message });
    await Sharing.shareAsync(uri, { message });
  } catch (error) {
    console.error('Oops, snapshot failed', error);
  }
};
return (
        <ViewShot
        style={{flex:1}}
        ref = {viewShot}
        options={{ format: "png", quality: 0.9 }}
        >
        <NavigationContainer>
            
            <Tab.Navigator
                         ......

Second code with core share module from react-native, expo-file-system, and react-native-view-shot, shares the message but not the screenshot:

const viewShot = useRef(null);

  const onShare = async () => {
    try {
      console.log('Sharing...');
      const uri = await takeScreenshot();
      console.log('URI:', uri);
      await Share.share({
        title: 'Share Screenshot',
        message: 'Check out this screenshot!',
        url: `file://${uri}`,
      });
      console.log('Shared successfully');
    } catch (error) {
      console.log(error.message);
    }
  };

  const takeScreenshot = async () => {
    try {
      console.log('Taking screenshot...');
      const uri = await viewShot.current.capture();
      console.log('Screenshot taken:', uri);
      const temporaryDirectory = FileSystem.documentDirectory + 'screenshot.png';
      await FileSystem.copyAsync({ from: uri, to: temporaryDirectory });
      console.log('Screenshot saved to:', temporaryDirectory);
      return temporaryDirectory;
    } catch (error) {
      console.log(error.message);
      throw error;
    }
  };

This functionality seems trivial but I still haven't managed to make it work. Thanks.

1

There are 1 best solutions below

1
On

I have extended the expo Take a Screenshot example as follows (my example):


import { useState, useRef } from 'react';
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, View } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import * as MediaLibrary from 'expo-media-library';
import { captureRef } from 'react-native-view-shot';

import Button from './components/Button';
import ImageViewer from './components/ImageViewer';
import CircleButton from './components/CircleButton';
import IconButton from './components/IconButton';
import EmojiPicker from './components/EmojiPicker';
import EmojiList from './components/EmojiList';
import EmojiSticker from './components/EmojiSticker';
import * as Sharing from 'expo-sharing';

const PlaceholderImage = require('./assets/images/background-image.png');

export default function App() {
  const [isModalVisible, setIsModalVisible] = useState(false);
  const [showAppOptions, setShowAppOptions] = useState(false);
  const [pickedEmoji, setPickedEmoji] = useState(null);
  const [selectedImage, setSelectedImage] = useState(null);

  const [status, requestPermission] = MediaLibrary.usePermissions();
  const imageRef = useRef();

  if (status === null) {
    requestPermission();
  }

  const pickImageAsync = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      allowsEditing: true,
      quality: 1,
    });

    if (!result.canceled) {
      setSelectedImage(result.assets[0].uri);
      setShowAppOptions(true);
    } else {
      alert('You did not select any image.');
    }
  };

  const onReset = () => {
    setShowAppOptions(false);
  };

  const onAddSticker = () => {
    setIsModalVisible(true);
  };

  const onModalClose = () => {
    setIsModalVisible(false);
  };

  const onShareImageAsync = async () => {
    try {
      const localUri = await captureRef(imageRef, {
        height: 440,
        quality: 1,
      });

      await Sharing.shareAsync(localUri, { mimeType: 'image/gif' });
    } catch (e) {
      alert(e.message);
    }
  };

  const onSaveImageAsync = async () => {
    try {
      const localUri = await captureRef(imageRef, {
        height: 440,
        quality: 1,
      });

      await MediaLibrary.saveToLibraryAsync(localUri);
      if (localUri) {
        alert('Saved!');
      }
    } catch (e) {
      console.log(e);
    }
  };

  return (
    <GestureHandlerRootView style={styles.container}>
      <View style={styles.imageContainer}>
        <View ref={imageRef} collapsable={false}>
          <ImageViewer
            ref={imageRef}
            placeholderImageSource={PlaceholderImage}
            selectedImage={selectedImage}
          />
          {pickedEmoji !== null ? (
            <EmojiSticker imageSize={40} stickerSource={pickedEmoji} />
          ) : null}
        </View>
      </View>
      {showAppOptions ? (
        <View style={styles.optionsContainer}>
          <View style={styles.optionsRow}>
            <IconButton icon="refresh" label="Reset" onPress={onReset} />
            <CircleButton onPress={onAddSticker} />
            <IconButton
              icon="save-alt"
              label="Save"
              onPress={onSaveImageAsync}
            />
            <IconButton
              icon="share"
              label="Share"
              onPress={onShareImageAsync}
            />
          </View>
        </View>
      ) : (
        <View style={styles.footerContainer}>
          <Button
            theme="primary"
            label="Choose a photo"
            onPress={pickImageAsync}
          />
          <Button
            label="Use this photo"
            onPress={() => setShowAppOptions(true)}
          />
        </View>
      )}
      <EmojiPicker isVisible={isModalVisible} onClose={onModalClose}>
        <EmojiList onSelect={setPickedEmoji} onCloseModal={onModalClose} />
      </EmojiPicker>
      <StatusBar style="auto" />
    </GestureHandlerRootView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#25292e',
    alignItems: 'center',
  },
  imageContainer: {
    flex: 1,
    paddingTop: 58,
  },
  footerContainer: {
    flex: 1 / 3,
    alignItems: 'center',
  },
  optionsContainer: {
    position: 'absolute',
    bottom: 80,
  },
  optionsRow: {
    alignItems: 'center',
    flexDirection: 'row',
    justifyContent: 'center',
  },
});