Send Image to Server

Asked

Viewed 587 times

1

When I send a byte[] converted to String to the server on the server comes empty or breaks. I don’t understand why.

  Bitmap photo = (Bitmap)data.getExtras().get("data");
                    photo = Bitmap.createScaledBitmap(photo, 800, 1422, false);
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.PNG, 100, bytes);

                    byte[] imageArray = bytes.toByteArray();

                    String imageAsString = Base64.encodeToString(imageArray, Base64.DEFAULT);

Then to send to the server do:

private String InsertImage(String url)
{
    HttpClient client = buildHttpClient();
    HttpPost request = new HttpPost(url);
    HttpResponse response = null;
    String responseString = "";

    try
    {
        System.out.println("VOU PARA O SERVIDOR");
        JSONObject jsonObject = this.setHeadersAndbuildJSONObject(request);

        System.out.println("vou levar 1 - " + sessionID);
        System.out.println("vou levar 2 - " + data[0]);
        System.out.println("vou levar 3 - " + data[1]);

        jsonObject.put("SessionGUID", sessionID);
        jsonObject.put("Address", data[0]);
        jsonObject.put("imageAsBytes", data[1]);

        responseString = this.executeRequest(jsonObject, request, response, client);
        System.out.println("VIM DO SERVIDOR " + responseString.toString());

    }
    catch (Exception e)
    {
        System.out.println("ERRO " + e.getMessage());
    }
    return responseString;
}

In the system.out you see the variables are all well filled. On the server side burst comes with the following error "Object Reference not set to an instance of an Object."

1 answer

1

  • the sending to the Server in this app was done so I will not be able to remodel the entire app in this direction that you pointed out. However it is not sent in byte[] but in String that goes inside an Object[].

  • Got it. What you should see is how this data should be encoded. In general, Base64 is used, but the developer of the app part may have used something else. Apparently the developer is expecting a serialized object, not a byteArray encoded in Base64. My suggestion is you see more details of how the server expects you to send the object, otherwise it will be a long shot ... trial and error ...

  • Thank you, on the server side he is waiting for a String and then there handles the String so to put the photo in Azurestorage so that’s why in the code after taking the photo I did the following "String imageAsString = Base64.encodeToString(imageArray, Base64.DEFAULT); " and sent the "imageAsString" variable to the server, but in this case it seems to burst

  • Precisely, gives error because it seems that it is not Base64 that he is waiting for you to send ...

  • I am sending in String though in Base64, and on the server side is waiting for a String I think Base64 does not have this error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.