I did it!
I think this is not the best way to do this, but it works, so I am fine until I get a better solution.
So, I take a Bitmap image and convert it to String:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); byte[] bitmapByte = outputStream.toByteArray(); String stringEncodedImage = Base64.encodeToString(bitmapByte, Base64.DEFAULT);
Then I create an httpPostRequest and set a JsonObject to it with the image converted to String in it.
HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("https://my_app_path/_ah/api/registration/v1/uploadImage"); JSONObject jsonObject = new JSONObject(); jsonObject.put("image",stringEncodedImage); StringEntity stringEntity = new StringEntity(jsonObject.toString()); httpPost.addHeader("Content-Type", "application/json"); httpPost.setEntity(stringEntity); HttpResponse response = httpClient.execute(httpPost);
On the server side, at my endpoint, I do this:
@ApiMethod(name = "uploadImage", httpMethod = "POST") public JSONObject uploadImage(JSONObject request) throws IOException { String imageInString = (String) request.get("image"); Blob blob = new Blob(imageInString.getBytes()); ....save blob and do whatever you want... }
The same thing happens differently. I will pack Blob in JsonObject and submit it.
source share