Problem adding image inside a Thread

0

I have this following class call inside the main is working perfectly single problem is for me to setar the image I received in the msg of the following error

  

E / AndroidRuntime: FATAL EXCEPTION: Thread-201       android.view.ViewRootImpl $ CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

 class LooperThread extends Thread {

    @Override
    public void run() {

      try {
        while (true) {
            System.out.println("entro");
            Fila fila = new Fila("NocView", "NocView-Campainha", TipoFila.Get, "rabbit01.spacnet.com.br", "campainha", "campainha");
            byte[] teste = fila.consome();
            Mensagem m = new Mensagem().getObjeto(teste);
            Campainha campainha2 = (Campainha) m;
            fila.ack();


            InputStream in = new ByteArrayInputStream(campainha2.getImagem());
            Drawable d = Drawable.createFromStream(in, "src name");

            byte[] data = campainha2.getImagem();
            Bitmap bmp = BitmapFactory.decodeByteArray(campainha2.getImagem(), 0, campainha2.getImagem().length);
            img.setImageBitmap(bmp);  -> erro

    }
}

How could you be solving this problem?

    
asked by anonymous 17.12.2014 / 17:54

1 answer

1

You can use an AsyncTask and put the image manipulation part inside it.

If processing is not heavy, you can use a UIThread, as in the example below:

 [Sua Activity].this.runOnUiThread(new Runnable() {
            @Override
            public void run() {

                //procesamento aqui
            }
        });

In this case, you need your Activity.

    
18.12.2014 / 13:26