Search code examples
react-nativevideobase64

How to convert video (from mobile storage) to base64 in react native?


I am getting video from mobile local storage through ImagePicker in react native, now I want to convert this video to base64 but unable to do it.

Implemented code is:

ImagePicker.launchImageLibrary(
  { mediaType: 'video', includeBase64: true },
  (response) => {
    try {
      RNFetchBlob.fs
        .stat(response.assets[0].uri)
        .then((res) => {
          //"res.path" will give me original path of video.
        })
        .catch((err) => {});
    } catch (Excepstion) {}
  }
);

Solution

  • Checkout the react-native-fs package. and use that like:

    import RNFS from 'react-native-fs';
    ...
    
    // readFile(filepath: string, encoding?: string)
    RNFS.readFile(filePath, 'base64').then(res => {
        ...
    })
    .catch(err => {
        ...
        console.log(err.message, err.code);
        ...
    });
    

    Hope this works for you.