我更改了Android BluetoothChat示例项目,以通过蓝牙传输文件。我成功地传输了文本文件并将它们打印在ListActivity上。我想用图像做同样的事情,但无法使它工作。有一个Handler对象将InputStream接收的字节数组发送到UserInterface。在那里,我需要将此bytearray转换为图像。我尝试了以下但不起作用:
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
myImg.setImageBitmap(BitmapFactory.decodeByteArray(readBuf, 0, readBuf.length));
//the following lines would actually display the text of the file in the ListActivity
//String readMessage = new String(readBuf, 0, msg.arg1);
//mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage);
break;
}
}
};
向UI发送消息的处理程序代码如下所示:
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1,buffer).sendToTarget();
ANSWER 我读了很多其他帖子,我发现其他人成功地使其发挥作用。他用了一个拯救我的小技巧。我不会在这里给出答案,因为它在问题页Sending image from android to PC via bluetooth
中有更好的解释感谢大家的帮助,但
答案 0 :(得分:0)
尝试使用BitmapFactory.decodeByteArray(byte[] data, int offset, int length)
方法从字节数组中获取位图。