문제

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!

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top