Skip to content

Instantly share code, notes, and snippets.

@iampato
Created March 5, 2023 09:43
Show Gist options
  • Save iampato/c4a2f6f635314e4e9abcca25173c055c to your computer and use it in GitHub Desktop.
Save iampato/c4a2f6f635314e4e9abcca25173c055c to your computer and use it in GitHub Desktop.
Download react native images
import RNFS from 'react-native-fs';
import Share from 'react-native-share';
import Progress from 'react-native-progress';
const downloadAndShareImage = async (imageUrl, imageName) => {
const downloadDest = `${RNFS.DocumentDirectoryPath}/${imageName}.jpg`;
// Download the image file
const options = {
fromUrl: imageUrl,
toFile: downloadDest,
progressDivider: 5, // Update the progress every 5%
background: true, // Continue downloading in the background even if app is closed
};
let progress = 0;
const downloadPromise = RNFS.downloadFile(options).promise;
// Show a progress indicator
const progressPromise = new Promise((resolve, reject) => {
const interval = setInterval(() => {
if (progress >= 1) {
clearInterval(interval);
resolve();
}
}, 100);
});
await Promise.all([downloadPromise, progressPromise]);
// Share the image file
const shareOptions = {
title: 'Share via',
message: 'Check out this image!',
url: `file://${downloadDest}`,
type: 'image/jpeg',
};
await Share.open(shareOptions);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment