Se-BASS-tian
Se-BASS-tian

Reputation: 51

How display bitmap in imageview from another thread (Android handler)

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();
    } 
} }

Upvotes: 1

Views: 2086

Answers (2)

mykolaj
mykolaj

Reputation: 974

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.

Upvotes: 2

Boban S.
Boban S.

Reputation: 1662

Use AsyncTask for network operations.

class ImageDownloader extends AsyncTask<Void, Void, Bitmap> {

    private String host;
    private int socketIn;
    private ImageView imageView;

    public ImageDownloader(String host, int socketIn, ImageView imageView) {
        this.host = host;
        this.socketIn = socketIn;
        this.imageView = imageView;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            InetAddress serwerAddress = InetAddress.getByName(host);
            Socket socket = new Socket(serwerAddress, socketIn);
            clientIn = new ClientIn(socket);
            bitmap = clientIn.Receive();

            return bitmap;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);

        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }
    }

}

Start it in onCreate with

new ImageDownloader(aHost, aSocketInInt, imageView).execute();

Upvotes: 2

Related Questions