Hi,

You have to put the while(isStop) loop in your Thread's run() method,
not inside the new Runnable that you post:

public MyThread extends Thread {
 Handler handler;
 public MyThread() {
   super();
    handler = new Handler();
 }
 public void run() {
   ..
   while(isStop){
   ...
   handler.post(new Runnable() {
     public void run() {
            imgData = getImage();  // imgData receive jpeg image data
            bm = BitmapFactory.decodeByteArray(imgData,
0,imgData.length); //
convert jpeg image into bitmap
            imgView.setBitmap(bm);
     }
   });
   ...
  }
  ...
 }
}

Anything that you need to run using the 'handler.post()' method must
run in a very short time, otherwise your user-input handling will not
work and your app will lock up. In your example, the while(isStop)
would not stop looping, causing the posted runnable to run almost
forever --> locking up your app.

You have to code your 'MyThread' class to accept jobs/requests from
your main UI-thread. Your thread will handle these requests and then
call the handler.post() method to show the results in form of a
different bitmap.

To do this can be a bit daunting if you don't have much Threading
experience. I suggest you taking a good look at the
java.util.concurrent.ExecutorService classes and the FutureTask
classes. These classes do a lot of that hard work already for you.

//Code in your main-thread:
ExecutorService executor = Executors.newSingleThreadExecutor();
...
...
// when you need to update the bitmap in the imgView:
FutureTaks<?> task = executor.submit(new Runnable() {
  public void run() {
    ... // do the work to figure out the bitmap
    imgData = getImage();  // imgData receive jpeg image data
    bm = BitmapFactory.decodeByteArray(imgData,0,imgData.length); //
get bitmap
    // post back to the main thread with the new bitmap.
    post(new Runnable() {
        public void run() {
            imgView.setBitmap(bm);
        }
    });
  }
});

Now 'task' holds the task being executed in a seperate thread by the
ExecutorService you created.
You can actually call 'cancel' on 'task' to cancel submitted jobs/
requests/etc. It's all done for you :)

On Mar 3, 2:00 am, 민승기 <[email protected]> wrote:
> Thank you for your answer !
>
> imgView.setBitmap(bm) is working very well without failure in thred routine
> as you described
>
> but I need refreshing of screen with new bitmap image data repeatedly.
>
> In addition to your code, for the purpose of refreshing new image data, I
> added infinite loop with while(isStop) operation.
>
> public MyThread extends Thread {
>  Handler handler;
>  public MyThread() {
>    super();
>     handler = new Handler();
>  }
>  public void run() {
>    ..
>    ...
>    handler.post(new Runnable() {
>      public void run() {
>        while(isStop){
>             imgData = getImage();  // imgData receive jpeg image data
>             bm = BitmapFactory.decodeByteArray(imgData,0,imgData.length); //
> convert jpeg image into bitmap
>             imgView.setBitmap(bm);
>        }
>      }
>    });
>    ...
>  }
>
> }
>
> then this program works with no error.
> While loop goes on...
> I can have right image data, but display never refresh with new image data.
> and program didn't response any kind of input such like a button.
>
> 2009/3/3 Streets Of Boston <[email protected]>
>
>
>
>
>
> > imgView.setBitmap can not be called from any other thread than the
> > main thread (i.e. thread on which you created your view).
>
> > do this instead:
>
> > handler.post(new Runnable() {
> >  public void run() {
> >    imgView.setBitmap(bm);
> >  }
> > });
>
> > The 'handler' can be created by just calling 'new Handler()' on the
> > main thread, e.g. when you create your worker-thread.
>
> > public MyThread extends Thread {
> >  Handler handler;
> >  public MyThread() {
> >    super();
> >     handler = new Handler();
> >  }
> >  public void run() {
> >    ..
> >    ...
> >    handler.post(new Runnable() {
> >      public void run() {
> >        imgView.setBitmap(bm);
> >      }
> >    });
> >    ...
> >   }
> > }
> > On Mar 2, 4:47 am, rex <[email protected]> wrote:
> > > Hi~
>
> > > I'm trying to periodically change image data in ImageView in the
> > > thread routine
> > > but when I put this code in the tread routine, program is killed.
>
> > >  imgView.setBitmap(bm);
>
> > > here is my thread code
> > > ...
>
> > > private byte[] imgData;
> > > private Bitmap bm;
>
> > > new Thread (new Runnable()
> > > {
> > >     public void run()
> > >     {
> > >         while(!isStop){
> > >                    imgData = getImage();
> > >                    bm = BitmapFactory.decodeByteArray(imgData,
> > > 0,imgData.length);
> > >                    imgView.setBitmap(bm); --> This caused error!!
> > >         }
> > >      }
>
> > > }).start();
>
> > > ...
>
> > > imgData is receiving jpeg image data from somewhere.
> > > Outside thread routine, image data is displayed well.
>
> > > If above is impossible, are there any other ways to periodically image
> > > data refreshing on screen ?
>
> > > Thanks!- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to