0

i am new to react native. I am using react-native-image-crop-picker to select image from gallery. On image select from gallery, it returns image path like this:

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

I want to convert this path into base64 string. I can not seem to find a proper way to do this. Please help me with this. Thanks in advance.

P.S. Also please mention how can i convert back from base64.

fahad
  • 1
  • 2
  • 1
    Does this answer your question? [React native Base64 encoding string](https://stackoverflow.com/questions/47255154/react-native-base64-encoding-string) – jps Sep 14 '21 at 17:07
  • There is a `includeBase64` flag that you can include in the request object. Perhaps that is what you want? – Moistbobo Sep 15 '21 at 10:56

2 Answers2

0

From path to image you can use React native fs. See more here

To convert it back unfortunately you will need to save it, but you can save it under the hood in the temp folder. More details here

Domotor Zsolt
  • 586
  • 5
  • 10
-2

Maybe try encodeURIComponent()? I know that it works with links so it may very well work with file paths

Try this:

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

And to decode ( Though in my experience it is usually not necessary ), use decodeURIComponent

Rami M
  • 82
  • 5