0

I have a photo (jpeg) format in an Android device. Instead of posting the file as a file using HTTP I prefer to convert that to a string and post it as string using http to a spreadsheet. I understand jpeg files are encoded and opening them as string shows funny characters. My question is if I send these characters as string using http, can I get them back on the other side using a binary file editor and save them as jpeg?

Amir
  • 1,667
  • 3
  • 23
  • 42
  • We do not know what you have in mind with sending as string. The way one usually does this is to base64 encode the bytes content of the jpg file to a string. The receiving side will then base64 decode the string to the original bytes. – greenapps May 29 '16 at 07:24
  • Posting / uploading a JPEG(or any binary)file, character by character might not be a good approach to do so. See [this](http://stackoverflow.com/a/20380256/2952723) – Harshiv May 29 '16 at 09:59
  • The reason I want to do this, I am sending the data to google drive where sending string doesn't need much configuration while sending files requires using APIs etc. – Amir Jun 01 '16 at 09:11

1 Answers1

0

One thing you can do is, on client side:

1.Convert image to byte array.

Path path = yourImageFile.toPath();
byte[] byteArray = Files.readAllBytes(path);

2.Encode the byteArray to Base64 String.

String encodedString = Base64.encodeBase64URLSafeString(byteArray);

3.That's it, send via http.

I'm not really sure what you mean by a binary file editor, but from server side you can retrieve the image like this (if using Java):

byte[] decodedByte = decodedBase64.decodeBase64(encodedString);
FileOutputStream out = new FileOutputStream("newPathToYourImage");
out.write(decodedByte);
out.close();
Joel Min
  • 3,387
  • 3
  • 19
  • 38