sonu thomas
sonu thomas

Reputation: 2161

How to refresh imageview in android

I want to refresh imageview on bytearray received from socket.

new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (!socket.isClosed()) {
                    imgArray = receiveImagebytes();
                }
            }
        }).start();

        try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


while (!socket.isClosed()) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                    imageView.setImageBitmap(BitmapFactory.decodeByteArray(imgArray, 0, imgArray.length));
                    imageView.invalidate();
                }
            });

imgArray is the bytearray received in another thread. I want to refresh the imageview..But it is not working..It is remaining with same default icon

Upvotes: 1

Views: 7996

Answers (2)

sonu thomas
sonu thomas

Reputation: 2161

Sorry to all.The problem was that the above code was in the main thread,thus got blocking all responses to the UI.

The problem got solved when I moved the code to a thread from the main thread.Now it is working correctly.

Thanks for all replies

Upvotes: 1

akkilis
akkilis

Reputation: 1934

Either the image is coming same all the time, or it is coming only once. Because if you have triggered the above code correctly from the right place, it must refresh the imageView.

Try to debug it in different manner, code to refresh is ok.

Upvotes: 1

Related Questions