How to send and receive bitmap on restful server

6

I searched and found very little content, I would like some tips because I never did. How do I send and receive a bitmap to a server? Command with JSon? Convert to base64? What is the best way to send and how should my method be on the server to receive? Thanks.

    
asked by anonymous 22.10.2015 / 19:29

2 answers

5
___ erkimt ___ How to send and receive bitmap on restful server ______ qstntxt ___

I searched and found very little content, I would like some tips because I never did. How do I send and receive a bitmap to a server? Command with JSon? Convert to base64? What is the best way to send and how should my method be on the server to receive? Thanks.

    
______ ___ azszpr93984

Simply submit as binary content.

String url = "http://seusite.com.br";
File file = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath(),"suaImagem.jpg");
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(url);

    InputStreamEntity reqEntity = new InputStreamEntity(
            new FileInputStream(file), -1);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true); // mande em pacotes separados se necessario
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);

} catch (Exception e) {
    // Trate seu erro
}

this response.

    
______ azszpr93967 ___

The correct thing is that you encode the base64 image and send it through the server, before sending, it converts the Base64 image to a Byte array and sends that entire Object.

String url = "http://seusite.com.br";
File file = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath(),"suaImagem.jpg");
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(url);

    InputStreamEntity reqEntity = new InputStreamEntity(
            new FileInputStream(file), -1);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true); // mande em pacotes separados se necessario
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);

} catch (Exception e) {
    // Trate seu erro
}
    
___
22.10.2015 / 21:25
4

The correct thing is that you encode the base64 image and send it through the server, before sending, it converts the Base64 image to a Byte array and sends that entire Object.

//Codificar uma imagem
Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(),
                    R.drawable.imagem);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitMapData = stream.toByteArray();
String encodedImage2 =Base64.encodeToString(bitMapData,Base64.DEFAULT);

//Decodificar
byte[] arquivo = null;
arquivo = Base64.decode(objeto.imagem.toString());
    
22.10.2015 / 20:30