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

From Dev

How to take real coordinates(X,Y) of Bitmap from imageView Android

From Dev

How to display the image from database in android imageview

From Dev

How to display the image from database in android imageview

From Dev

android: ImageView not displaying a bitmap from URL

From Dev

Loading Bitmap to ImageView from URL in android

From Dev

Android draw area from Bitmap or Imageview with canvas

From Dev

android: ImageView not displaying a bitmap from URL

From Dev

Yet another draw bitmap from clipboard thread

From Dev

How to access objects from another thread android

From Dev

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

From Dev

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

From Dev

Android center Bitmap in ImageView

From Dev

Android - Update Bitmap from timer thread

From Dev

how to half overlap imageview on another imageview in android

From Dev

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

From Dev

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

From Dev

Use of unassgined variable, how do I return a bitmap from another thread?

From Dev

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

From Dev

How to compress bitmap from 10mb image from camera to 300kb beforw setting to imageview in android

From Dev

android display image in imageview from camera

From Dev

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

From Dev

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

From Dev

Android get Position of Bitmap in ImageView

From Dev

Android get Position of Bitmap in ImageView

From Dev

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

From Dev

How to display large size bitmaps in imageview android?

From Dev

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

From Dev

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

From Dev

Base64 to Bitmap to display in ImageView

Related Related

  1. 1

    How to take real coordinates(X,Y) of Bitmap from imageView Android

  2. 2

    How to display the image from database in android imageview

  3. 3

    How to display the image from database in android imageview

  4. 4

    android: ImageView not displaying a bitmap from URL

  5. 5

    Loading Bitmap to ImageView from URL in android

  6. 6

    Android draw area from Bitmap or Imageview with canvas

  7. 7

    android: ImageView not displaying a bitmap from URL

  8. 8

    Yet another draw bitmap from clipboard thread

  9. 9

    How to access objects from another thread android

  10. 10

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

  11. 11

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

  12. 12

    Android center Bitmap in ImageView

  13. 13

    Android - Update Bitmap from timer thread

  14. 14

    how to half overlap imageview on another imageview in android

  15. 15

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

  16. 16

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

  17. 17

    Use of unassgined variable, how do I return a bitmap from another thread?

  18. 18

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

  19. 19

    How to compress bitmap from 10mb image from camera to 300kb beforw setting to imageview in android

  20. 20

    android display image in imageview from camera

  21. 21

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

  22. 22

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

  23. 23

    Android get Position of Bitmap in ImageView

  24. 24

    Android get Position of Bitmap in ImageView

  25. 25

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

  26. 26

    How to display large size bitmaps in imageview android?

  27. 27

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

  28. 28

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

  29. 29

    Base64 to Bitmap to display in ImageView

HotTag

Archive