How to refresh imageview in android

前提是你 提交于 2019-12-13 19:52:04

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/8703407/how-to-refresh-imageview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!