akexorcist / BluetoothSPPLibrary

[UNMAINTAINED][Android] Bluetooth Serial Port Profile which comfortable to developer application to communication with microcontroller via bluetooth
Apache License 2.0
1.7k stars 570 forks source link

Create a Bitmap from byte[]. Received Data. #72

Closed rafaelcrz closed 7 years ago

rafaelcrz commented 7 years ago

User 1 takes a photo from camera, get it and send to other user.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");

                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                byte[] bitmapByte = outputStream.toByteArray();

                bluetoothSPP.send(bitmapByte, true); **//Send the Bitmap array**
            }
        }

User 2 receive the Bitmap byte[].

 bluetoothspp.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
            public void onDataReceived(byte[] data, String message) {

                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                Glide.with(ChatActivity.this).load(bitmap ).asBitmap().into(imgFundo); //Use Glide for show Bitmap
            }
    });

But I always get an error and Glide can not display the image. I did a debug and the data comes in several parts, like an array of bytes.

Thanks

bhorgaurav commented 7 years ago

Does onActivityResult.bitmapByte.length match setOnDataReceivedListener.data.length ? If not, that's your issue. The data might be fragmented.