繁体   English   中英

将字节数组转换为Android位图

[英]convert byte array to Bitmap Android

我将Android BluetoothChat示例项目更改为通过蓝牙传输文件。 我成功传输了文本文件,并将它们打印在ListActivity上。 我想对图像执行相同的操作,但无法使其正常工作。 有一个Handler对象,它将在InputStream处接收到的字节数组发送到UserInterface。 在那里,我需要将此字节数组转换为图像。 我尝试了以下操作,但不起作用:

    @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();

解答我阅读了许多其他文章,然后发现其他人成功地使它起作用。 他使用了一个小技巧,救了我。 我不会在这里给出答案,因为它在问题页面中以更好的方式进行了说明, 通过蓝牙将图像从android发送到PC

谢谢大家的帮助

尝试使用BitmapFactory.decodeByteArray(byte[] data, int offset, int length)方法从字节数组获取位图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM