Created
March 5, 2023 09:43
-
-
Save iampato/c4a2f6f635314e4e9abcca25173c055c to your computer and use it in GitHub Desktop.
Download react native images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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