Pergunta

I have the following code to send an image (bitmap binary data) using android http default client:

//gets raw binary data and convert it to a string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageData = new String(imageBytes);
//http put body
StringEntity se = new StringEntity(imageData);
putRequest.setEntity(se);

I need to send its raw binary content in the put request body, but it seems that the content is not being properly sent.

I can send an image binary using curl with the --data-binary (that's what I am trying to do in android).

Is the process in converting the bitmap binary to string correct?

Thanks!

Foi útil?

Solução

Well, my bad.

In case anyone makes the same mistake:

I should have used the ByteArrayEntity class instead of the StringEntity one:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
//http put body
ByteArrayEntity be = new ByteArrayEntity(imageBytes);
putRequest.setEntity(be);

I was trying to convert the bytearray into a string and send it, the correct way to do it is to use the ByteArrayEntity class.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top