如何在 React Native 中将视频(从移动存储)转换为 base64?

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

我在react native中通过ImagePicker从移动本地存储中获取视频,现在我想将此视频转换为base64但无法执行。

实现的代码是:

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) {}
  }
);

签出 react-native-fs 包。并像这样使用:

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);
    ...
});

希望这对你有用。