Android Byte Array Packages

I go through Bluetooth grammatically. When I send an image as an array of bytes on the sending side, the length of the byte array is = 83402, and on the receiving side, I get byte bytes 1024.

I want to combine these 1024 batches into one byte in order to convert it again as image quality.

Here in msg.obj I get 1024 bytes of an array of bytes.

case MESSAGE_READ:

byte[] readBuf = (byte[]) msg.obj; Bitmap bmp=BitmapFactory.decodeByteArray(readBuf,0,readBuf.length); 

After that I get this warning ..

"The default buffer size used in the BufferedOutputStream constructor. It is better to be explicit if an 8k buffer is required."

Any help would be appreciated.

thanks

+6
source share
2 answers

There should be 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); 
+1
source

I'm going to go limb here and assume that you are using the bluetoothChat example from sdk to create your image sender (all of your examples are consistent with this). Here, the quick conversion that I dumped together may not be the best, but it works.

You get them in batches of 1024, because in the BluetoothChatService.java launch function, it creates a buffer size of 1024, which goes and receives information from the input stream. If you create another buffer that matches the image there (I set max 1mb), then your launch function will have:

 byte[] buffer = new byte[1024]; byte[] imgBuffer = new byte[1024*1024]; int pos = 0; 

with your pos variable tracking where you are in imgBuffer.

Then you just copy it until you get the pieces in the while (true) loop of an image like this (mmInStream - InputStream):

 int bytes = mmInStream.read(buffer); System.arraycopy(buffer,0,imgBuffer,pos,bytes); pos += bytes; 

I am sending a message to tell him that the image has been sent, and at that moment move imgBuff to another thread (pos is the size of imgBuffer at this point):

 mHandler.obtainMessage(BluetoothChat.IMAGE_READ, pos, -1, imgBuffer) .sendToTarget(); 

I defined IMAGE_READ to decode the array, as you did in your MESSAGE_READ:

 byte[] readBuf = (byte[]) msg.obj; BitmapFactory.decodeByteArray(readBuf, 0, msg.arg1); 
0
source

Source: https://habr.com/ru/post/902388/


All Articles