Download video file directly to android device gallery using react-native-fs

297 Views Asked by At
 const downloadRelfile = (fileUrl) => {
    let date = new Date();
    const url = fileUrl;
    const myAlbumPath = RNFS.PicturesDirectoryPath + '/Pictpath'
    RNFS.mkdir(myAlbumPath)
    .then(() =>{
    const downloadD = RNFS.PicturesDirectoryPath + '/Pictpath/' + 'file_' + Math.floor(date.getTime() + date.getSeconds() / 2) + '.mp4'
    RNFS.downloadFile({
        fromUrl: url,
        toFile: downloadD,
    }).promise.then((res) => {
        console.log('File downloaded successfully', res);
        ToastAndroid.showWithGravity(
            "Video downloaded sucessfully",
            ToastAndroid.LONG,
            ToastAndroid.CENTER
        );
    }).catch((err) => {
        console.log('Error downloading file:', err);
    });
})
};

I used this code but it works as like but the downloaded file not appeared in gallery in android, Now I want to download the video file in gallery directly using react-native-fs; Please help...

1

There are 1 best solutions below

0
Juan Frontons On

It will never appear in the library because you are saving it in a temporary app directory, you have to access the native media library from iOS/Android to save it properly. If you are using expo you can use: https://docs.expo.dev/versions/latest/sdk/media-library/#medialibrarysavetolibraryasynclocaluri

Like this:

await MediaLibrary.saveToLibraryAsync('file://' + downloadD);