显示位图setImageBitmap

时间:2015-04-01 15:59:28

标签: android

在我的应用程序Android中,我试图从带有套接字的服务器接收一些图像,但我无法使用setImageBitmap()显示它们。我没有任何错误,但我的屏幕保持白色。 这是我的代码:

public class CommunicateurAndroid implements Runnable {
    public CommunicateurAndroid(Socket s, ImageView img) {
        _imageV = img;
        try {
            _din = new DataInputStream(s.getInputStream());
            _dout = new DataOutputStream(s.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        new Thread(this).start();
    }

    public void run() {

        int length = 0;
        byte tmp;
        while (true) try {
            length = _din.readInt();
            byte tab[] = new byte[length];
            for (int i = 0; i < length; i++) {
                tmp = _din.readByte();
                tab[i] = tmp;
            }
            _image = BitmapFactory.decodeByteArray(tab, 0, tab.length);
            System.out.println("----------");
            if (_image != null) {
                _imageV.setImageBitmap(_image);
                System.out.println("TEST");
                Thread.sleep(5000);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public Bitmap get_image() {
        return _image;
    }

    private DataInputStream _din;
    private DataOutputStream _dout;
    private Bitmap _image;
    private ImageView _imageV;

}

我用这一行调用构造函数
    new CommunicateurAndroid(_socket, (ImageView) findViewById(R.id.ImageView3_Left));

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

从服务器接收图像并显示图像的最佳方法是使用Android-Universal-Image-Loader库。

我在我的应用程序中使用它并且是解决问题的最佳解决方案。