[英]Received byte array containing bitmap returns null
我一直在嘗試使用 Google 的 BluetoothChat 示例將圖像從一台設備發送到另一台設備。 我可以從一個設備向另一個設備發送字符串值,但我在發送圖像時遇到了問題。 我有兩個類處理數據的接收和發送。
為了發送圖像,我將圖像路徑轉換為 bitmap,然后將其轉換為 byte[],並將其傳遞給實用程序 class,與 BluetoothChat 示例相同,但緩沖區大小增加(默認為 1024,將其更改為 8192)。 我的 BluetoothSend class 將數據發送到實用程序 class 的代碼是這樣的,
send.setOnClickListener(view -> {
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
int width = bitmap.getRowBytes();
int height = bitmap.getHeight();
int bmpSize = width * height;
ByteBuffer byteBuffer = ByteBuffer.allocate(bmpSize);
bitmap.copyPixelsToBuffer(byteBuffer);
byte[] byteArray = byteBuffer.array();
sendUtils.write(byteArray);
bitmap.recycle();
});
這是處理數據發送和接收的實用程序 class,
/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket bluetoothsocket;
private final InputStream inputStream;
private final OutputStream outputStream;
public ConnectedThread(BluetoothSocket socket) {
bluetoothsocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = bluetoothsocket.getInputStream();
tmpOut = bluetoothsocket.getOutputStream();
} catch (IOException e) {
Log.e("ConnectedThrd->Cons", "Socket not created.");
e.printStackTrace();
}
inputStream = tmpIn;
outputStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[8192]; //1024 original
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
try {
bytes = inputStream.read(buffer);
// Send the obtained bytes to the UI Activity
handler.obtainMessage(BluetoothSend.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
handler.obtainMessage(BluetoothReceive.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (NullPointerException n) {
Log.e("ConnectedThrd->Run", n.getMessage());
}
} catch (IOException e) {
Log.e("ConnectedThrd->Run", "Connection Lost.", e);
e.printStackTrace();
connectionLost();
break;
}
}
}
public void write(byte[] buffer) {
try {
try {
outputStream.write(buffer);
handler.obtainMessage(BluetoothSend.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
handler.obtainMessage(BluetoothReceive.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (NullPointerException n) {
Log.e("ConnectedThrd->Write", "Bluetooth Socket is null: " + n.getMessage());
}
} catch (IOException e) {
Log.e("ConnectedThread->Write", "Empty write stream.");
}
}
public void cancel() {
try {
bluetoothsocket.close();
} catch (IOException e) {
Log.e("ConnectedThread->Cancel", "Failed to close socket.");
}
}
}
最后,在 BluetoothReceive class 上,我使用處理程序接收數據。 處理程序 object 的代碼如下,
case MESSAGE_READ:
//Read message from sender
byte[] bufferRead = (byte[]) message.obj;
//bitmap decodedByte is null
Bitmap decodedByte = BitmapFactory.decodeByteArray(bufferRead, 0, bufferRead.length);
int height = decodedByte.getHeight();
int width = decodedByte.getWidth();
Bitmap.Config config = Bitmap.Config.valueOf(decodedByte.getConfig().name());
Bitmap bitmap_tmp = Bitmap.createBitmap(width, height, config);
ByteBuffer buffer = ByteBuffer.wrap(bufferRead);
bitmap_tmp.copyPixelsFromBuffer(buffer);
fileView.setImageBitmap(bitmap_tmp);
break;
當我嘗試轉換從其他設備接收的字節數組時,我似乎總是得到一個 null 值,當我將它轉換為 bitmap 時,我可以用它在 ImageView 中顯示它。
我究竟做錯了什么?
如您所說,您可以傳遞字符串。 您可以將 bitmap 轉換為Base64
字符串格式並發送。
Bitmap bitmap = BitmapFactory.decodeFile("filePath");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT);
在接收端,反轉它(字符串Base64
到圖像)
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.