Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ByteBuffer to byte array

I have a ByteBuffer, and I need to add its content to a POST request in order to remotely display the image which represents; but I can't figure it out how to do it.

Doing this doesn't work:

byteBuffer.array()

I have tried to convert it to an array of bytes, but it didn't work because the image wasn't showing properly:

byte[] byteArray = new byte[byteBuffer.remaining()];
frame.duplicate().get(byteArray);
HttpResponse response = null;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
post.setEntity(new ByteArrayEntity(byteArray));

Also I've think about converting it to an image and get its array of bytes but I don't know how to do it and if it would work...

Any help will be appreciate.

like image 325
svprdga Avatar asked Sep 18 '26 00:09

svprdga


1 Answers

private static byte[] getByteArrayFromByteBuffer(ByteBuffer byteBuffer) {
    byte[] bytesArray = new byte[byteBuffer.remaining()];
    byteBuffer.get(bytesArray, 0, bytesArray.length);
    return bytesArray;
}
like image 92
Salman Nazir Avatar answered Sep 20 '26 14:09

Salman Nazir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!