【问题标题】:convert image path to base64 - React Native将图像路径转换为 ​​base64 - React Native
【发布时间】:2021-11-09 21:02:16
【问题描述】:

我是原生反应的新手。我正在使用react-native-image-crop-picker 从图库中选择图像。在从图库中选择图像时,它会返回如下图像路径:

file:///storage/emulated/0/Android/data/com.connect/files/Pictures/da154e60-77e0-4364-96a7-ad7fa5acb951.jpg

我想将此路径转换为 ​​base64 字符串。我似乎找不到合适的方法来做到这一点。请帮我解决一下这个。 提前致谢。

附:另请提及如何从 base64 转换回来。

【问题讨论】:

标签: javascript reactjs react-native base64


【解决方案1】:

也许试试encodeURIComponent()?我知道它适用于链接,因此它可以很好地适用于文件路径

试试这个:

console.log(encodeURIComponent('file:///storage/emulated/0/Android/data/com.connect/files/Pictures/da154e60-77e0-4364-96a7-ad7fa5acb951.jpg'))

要解码(虽然根据我的经验通常没有必要),请使用decodeURIComponent

【讨论】:

  • 这不会将路径/图像转换为 base64。
【解决方案2】:

从路径到图像,您可以使用React native fs。 查看更多here

不幸的是,要将其转换回来,您需要将其保存,但您可以将其保存在 temp 文件夹中。 More details here

【讨论】:

    猜你喜欢
    • 2022-10-17
    • 2018-06-14
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    相关资源
    最近更新 更多