接收字节[]和Android版的ImageView显示它 [英] Receive byte[] and display it on ImageView android

查看:100
本文介绍了接收字节[]和Android版的ImageView显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im做一个服务器/客户端实现,我需要从服务器发送一个字节[](图像已经转换)到客户端。

Im doing an Server/Client implementation and i need to send from Server a byte[] (image already converted) to the client.

我有服务器在发送和接收端的字节[],但我不能画在我的ImageView的。

I have the server sending, and the client receiving the byte[], but i cant paint it on my imageView.

我有以下的code客户端

I have the following code for client

类ViewerThread延伸AsyncTask的虚空,虚空,虚空> ...

class ViewerThread extends AsyncTask Void, Void, Void>...

public void startSharing(ImageView img_viewer) {

try {
            ObjectInputStream in_client = null;
            try {

                byte[] byteArray;
                Bitmap bmp;

                while (true) {
                    in_client = new ObjectInputStream(socket.getInputStream());
                    Log.e(TAG_THREAD, "Waitting for new image...");
                    //Log.e(TAG_THREAD, "Viewer: -> trying to read.....");
                    //Read byte[] -> image to draw
                    byteArray = (byte[])in_client.readObject();
                    Log.e(TAG_THREAD, "Received! -> " + byteArray.toString() + " size: "+ byteArray.length);

                    bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
                    //-> draw
                    Log.e(TAG_THREAD, "drawing... -> " + byteArray.toString() + " size: "+ byteArray.length);

                    img_viewer.setImageBitmap(bmp);
                    img_viewer.refreshDrawableState();
                }

            } catch (Exception e) {
                System.err.println("Server - Error transfering bitmaps: "
                        + e.getMessage());
            } finally {
                in_client.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
}

寻找LogCat中我有:

Looking for LogCat i have:

Waitting for new image...
Received! -> [B@4052b700 size: 293438
drawing... -> [B@4052b700 size: 293438

但它不从这里移动......只有当我停止服务器,图像出现在我的ImageView对象。

But it doesnt move on from here... Only if i stop the server, the image appears in my ImageView object.

活动:

img_viewer = (ImageView) findViewById(R.id.img_viewer);

ViewerThread vt = new ViewerThread(getString(R.string.server_address), 
            getString(R.string.port_number));

vt.execute();

Log.e("tag", "Viewer is connected to server!");

vt.startSharing(img_viewer);

还有什么比这里是什么问题?

What could be the problem here?

推荐答案

从code例子还不清楚,但我认为这code在UI线程中运行。

Not clear from your code example but I think this code runs in the UI thread.

只要你的code是在while循环用户界面无法刷新画面。一旦你停止服务器,你得到你捕获异常。这使while循环,并允许重绘自身的UI。因此,你可以看到那么图像。

As long as your code is in the while loop the user interface can't refresh the picture. As soon as you stop the server you get an exception that you catch. This leaves the while loop and allows the UI to redraw itself. Therefor you can see then the image.

这篇关于接收字节[]和Android版的ImageView显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆