I'm downloading the image from the server via TCP in another thread and I want to display it in the ImageView in activity. I created the handler, but does not work properly (does not show me the downloaded image). What is wrong ?
EDITED CODE:
public class TcpClient extends Activity {
ImageView imageView;
public static String aHost;
public String aSocketIn;
public static int aSocketInInt;
private Handler uiHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle_result);
imageView = (ImageView) findViewById(R.id.imageView);
Intent intent = getIntent();
aHost = intent.getStringExtra("addressIp");
aSocketIn = intent.getStringExtra("socketIn");
aSocketInInt = Integer.parseInt(aSocketIn);
uiHandler = new Handler();
ClientInThread clientInThread = new ClientInThread(aHost,aSocketInInt,uiHandler,imageView);
(new Thread(clientInThread)).start();
} }
class ClientInThread extends Thread implements Runnable {
public Bitmap bitmap = null;
public Handler handler;
String Host;
int SocketIn;
ImageView imageView;
ClientIn clientIn;
ClientInThread(String Host, int SocketIn, Handler handler, ImageView imageView) {
this.Host = Host;
this.SocketIn = SocketIn;
this.handler = handler;
this.imageView = imageView;
}
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
try {
InetAddress serwerAddress = InetAddress.getByName(Host);
Socket socket = new Socket(serwerAddress, SocketIn);
clientIn = new ClientIn(socket);
bitmap = clientIn.Receive();
}
catch (Exception e) {
e.printStackTrace();
}
} }
You've misused the Handler. Instead of using the handler's handleMessage
method you should obtain a handler for the UI thread like this:
...
private Handler uiHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle_result);
...
uiHandler = new Handler();
...
}
...
public void run() {
try {
InetAddress serwerAddress = InetAddress.getByName(Host);
Socket socket = new Socket(serwerAddress, SocketIn);
clientIn = new ClientIn(socket);
bitmap = clientIn.Receive();
} catch (Exception e) {
e.printStackTrace();
}
if (bitmap == null) {
// Downloading error
return;
}
// When a bitmap is downloaded you do:
uiHandler.post(new Runnable() {
imageView.setImageBitmap(bitmap);
});
...
}
If you're not restricted to use a third party libraries than you could use a really cool library UniversalImageLoader for this kind of tasks. Take a look here https://github.com/nostra13/Android-Universal-Image-Loader.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments