I am sending image through Bluetooth pro-grammatically. When i send image as Byte array at the sending side the byte array length is = 83402 and at the receiving side i am getting byte bacthes of 1024.
I want to combine these 1024 batches into single byte array so that i again convert it as an image.
Here in the msg.obj i get 1024 bacth of byte array.
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
Bitmap bmp=BitmapFactory.decodeByteArray(readBuf,0,readBuf.length);
After that i am also getting this warning..
"Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required"
any help would be appreciated.
Thanks
The standard definition of a byte is a data type that contains 8 bits. With 8 bits, a byte can hold values between zero and 255.
The byte[] is just a primitive array, just containing the raw data. So, it does not have convenient methods for building or manipulating the content. A ByteBuffer is more like a builder. It creates a byte[] .
We can use String class getBytes() method to encode the string into a sequence of bytes using the platform's default charset. This method is overloaded and we can also pass Charset as argument.
java.io.ByteArrayOutputStream. This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString() . Closing a ByteArrayOutputStream has no effect.
Should be roughly something like this:
byte[] readBuf = new byte[83402]; // this array will hold the bytes for the image, this value better be not hardcoded in your code
int start = 0;
while(/*read 1024 byte packets...*/) {
readBuf.copyOfRange((byte[]) msg.obj, start, start + 1024); // copy received 1024 bytes
start += 1024; //increment so that we don't overwrite previous bytes
}
/*After everything is read...*/
Bitmap bmp=BitmapFactory.decodeByteArray(readBuf,0,readBuf.length);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With