Skip to content

Instantly share code, notes, and snippets.

@mshivam019
Created February 14, 2024 19:38
Show Gist options
  • Save mshivam019/7f177f55a9a8acc2be44a13fb3b9d9e7 to your computer and use it in GitHub Desktop.
Save mshivam019/7f177f55a9a8acc2be44a13fb3b9d9e7 to your computer and use it in GitHub Desktop.
Sharing an image on react native
import Share from 'react-native-share';
import {CameraRoll} from '@react-native-camera-roll/camera-roll';
const shareImage = async (platform = 'default',uri) => {
try {
const shareOptions = {
title: 'title',
message: `message`,
url: uri,
filename: 'yourImage',
type: 'image/*',
};
if (platform === 'default') {
if (Platform.OS === 'ios') {
delete shareOptions.message;
}
await Share.open(shareOptions);
return;
} else if (platform === 'twitter') {
const isTwitterInstalled = await Linking.canOpenURL(
'twitter://user?screen_name=mshivam0019',
);
if (!isTwitterInstalled) {
return;
}
shareOptions.social = Share.Social.TWITTER;
} else if (platform === 'instagram') {
if (Platform.OS === 'ios') {
const fd = await CameraRoll.save(uri, {type: 'photo'});
const last = await CameraRoll.getPhotos({
first: 1,
assetType: 'All',
});
shareOptions.url = last.edges[0].node.image.uri;
}
const isInstagramInstalled = await Linking.canOpenURL(
'instagram://user?username=mshivam019',
);
if (!isInstagramInstalled) {
return;
}
shareOptions.social = Share.Social.INSTAGRAM;
} else if (platform === 'whatsapp') {
const isWhatsappInstalled = await Linking.canOpenURL(
'whatsapp://send?text=hello',
);
if (!isWhatsappInstalled) {
return;
}
shareOptions.social = Share.Social.WHATSAPP;
} else if (platform === 'download') {
// Save to camera roll
await CameraRoll.save(uri, {type: 'photo'});
//You can trigger a toast or alert to show the user that the image has been saved to camera roll
return;
}
await Share.shareSingle(shareOptions);
} catch (e) {
console.log(e);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment