1

I wrote an Android app to send some text (which should be saved in a file) and an image (which should be saved as a seperate file) with a HTTP-POST-request using the volley lib.

The image is converted to a byte array. This byte array is converted to a string using Base64.encodeToString(). This string is set as attribute in an JSON-object which is send to the server.

The interface on the server-side is a node-red-server: The data is received and the file names and the text are extracted. So far everything works fine. But when saving the image (which is a Base64-encoded string) only writes the string in a jpg-file.

What's left to do is decode the image string and save it as image. I don't know how to do this. I tried this approach, but node-red does not know Image().

Also I did not fully understand, why the best way to send an image is to parse it to a byte-array and encode this array as Base64-string. If someone could explain the idea or link me a tutorial this might help me.

Please let me know if you need my app-code/node-red-flows to help.

2
  • We can't help you with why you chose to base64 encode the image before sending it. Commented Sep 6, 2017 at 12:00
  • well... you are right, but most tutorials I found did it this way. So I thought it's done for a reason Commented Sep 6, 2017 at 12:03

2 Answers 2

2

There is a base64 Node-RED node (node-red-node-base64) that will convert a base64 encoded string into a NodeJS binary buffer.

If you run your string through this before sending it to a file node you will get a correctly formatted image.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the fast answer. I tried this node, but I still can't open the images. I pass the image-string as payload to this node. the resulting payload should be the content of the jpg-file. Did I understand that correct?
I tried to open the files and my programm told me "Error interpreting JPEG image file (Not a JPEG file: starts with 0x4c 0x7a)". If the image I capture is a jpg and I encode it and send it to the server it should still be a jpg, shouldn't it? I got this error with two different images
Assuming what you encoded on the Android side is a properly formatted jpeg stream then this should do what you need.
after encoding the image to a string I just decoded it again and displayed it in an image view. The encoding seems to be correct.
I solved this. thanks for your help, but it is important that the node takes a single line string as input!
0

I solved this. For everyone else who is looking for an answer I post this:

The base64 Node-RED node hardillb mentioned in his answer works fine, but only if the whole string is in one line.

If you use the encodeToString() function with the Base64.DEFAULT flag like this: String returnString = Base64.encodeToString(bytes, Base64.DEFAULT); Your recieve a multi line string. If you use the flag Base64.NO_WRAP instead it works fine with the node.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.