Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android byte array batches

Tags:

java

android

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

like image 603
Android_D Avatar asked Nov 25 '11 12:11

Android_D


People also ask

How much can a byte array hold?

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.

What is the difference between ByteBuffer and byte array?

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[] .

How do you assign a byte array?

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.

What is byte array in Android Studio?

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.


1 Answers

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);
like image 89
Caner Avatar answered Oct 04 '22 20:10

Caner