Question

I'm trying to take a photo with the Android camera. I need to wait 1 second or so when the program first starts so that Preview can be initiated and the photo can be taken. I did that with Handler.postDelayed and it works fine.

Now, my issue is I'd like to PAUSE the flow of the program until the run() gets executed, in which I used a while(true) loop with a flag to signal that the method has finished. However, the program freezes. There's no error returned. Can anyone shed some lights ? Below is my code

 flag = false;
 handler.postDelayed(new Runnable() { 
             public void run() 
             { 
                 preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
                 preview.camera.startPreview();
                 flag = true;
             } 
        }, 1000);

        while (true)
        {
            if (flag) break;
        }
Était-ce utile?

La solution

Remove the while(true) and it should continue, let me explain:

Android has a queue of tasks, so when finishs the current task it will go to the next task, so in your case the PostDelayed will be executed after it finishs the current Infinity loop, which will never be done, because the loop is blocking your Runnable in the queue.

so the best way is remove the Infinity loop, and in the end of your runnable call a method that continues your flow or a listener.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top