Send image to Server

1

When I send a [] byte converted to String to the server on the server it comes empty or bursts. I do not 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 I 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 system.out you see the variables are all well filled.

    
asked by anonymous 05.12.2016 / 19:29

1 answer

0

Hello! Are you sending the byte image [] to the server for some special reason?

Why do not you use an http lib to send the file over HTTP POST? It will be much simpler, and you do not have to invent the wheel, serialize and deserialize, etc.

In the link below you have an example ready exactly of what you need:

link

    
05.12.2016 / 20:05