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

Se-BASS-tian

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

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.

edited at
0

Comments

0 comments
Login to comment

Related

Screencapture:get Bitmap from Textureview and display in imageview android

How to get a Bitmap from an ImageView

How to display the image from database in android imageview

android: ImageView not displaying a bitmap from URL

Android draw area from Bitmap or Imageview with canvas

How to store background image from imageView in bitmap?

How to access objects from another thread android

Android : Converting imageview to bitmap, to grayscale, bitmap to imageview

How to display an image in android ImageView from the <img> tag

Android - Update Bitmap from timer thread

how to half overlap imageview on another imageview in android

How to move from one fragment to another fragment on click of an ImageView in Android?

How to Drag a bitmap over another Bitmap on Canvas with Surfaceview in Android

Android: Display ImageView from 2 different sources

android display image in imageview from camera

How to crop a bitmap from bottom part and show it imageview

How to pass a image dynamically from one activity to another activity and display it in imageview through JSON

Android get Position of Bitmap in ImageView

Android Studio: Bitmap to ImageView Error

Android - How to change UI from another thread in signed mode

How to display an ImageView on the whole screen on android?

How to generate bitmap one after another from y axis in android game

Base64 to Bitmap to display in ImageView

How to display an image from the Gallery in an ImageView?

How to display an imageview from a url with holder

How to check if ImageView contains Bitmap or not?

android ImageView how to move to another Layout

How to change android code from compress bitmap to reszie or scaled bitmap?

Android how to display image in Arraylist which intent from another activity?