android – How to send and receive bitmap on restful server

Question:

Guys, I did some research and found very little content, I would like some tips because I've never done that. How do I send and receive a bitmap to a server? Do I order with JSon? Do I convert to base64? What is the best way to send and how should my method on the server be to receive? It cost

Answer:

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
}

Example adapted from this answer .

Scroll to Top