Why pressing the take/back button isn't responsive enough?


#1

So I noticed pressing Take/Back button takes more than 3 seconds to respond and get back to CrimeFragment. Is it because we are running the camera on ui thread or am I missing something?


#2

The takePicture method is asynchronous, so the time delay here is not due to running this code on the UI thread. Instead, it’s due to the time it takes to actually capture and process the complete image from the camera. Different devices will take a different amount of time here.

Now, we do save the picture to the file system within that callback. In a real app, you would probably want to throw that on an AsyncTask or other background thread. It would not result in a dramatic time savings, though, just smoother graphics performance and perceived UI responsiveness.


#3

[quote=“phillips”]The takePicture method is asynchronous, so the time delay here is not due to running this code on the UI thread. Instead, it’s due to the time it takes to actually capture and process the complete image from the camera. Different devices will take a different amount of time here.

Now, we do save the picture to the file system within that callback. In a real app, you would probably want to throw that on an AsyncTask or other background thread. It would not result in a dramatic time savings, though, just smoother graphics performance and perceived UI responsiveness.[/quote]

Thanks for the reply, but based on the Chapter 19 code, we aren’t calling any takePicture method. All we do when the button is pressed is call getActivity().finish() which i believe just releases the mCamera so there isn’t much processing involved; But it takes rather long to finish the activity and return to CrimeFragment. I’m testing on a HTC sensation btw, maybe not the fastest device but still :slight_smile:


#4

Ahh, my apologies. I incorrectly assumed that you were referring to the next chapter.

Yes, the delay here comes from calling Camera.close() on the main thread. Both Camera.open() and Camera.close() take a bit of time, so in a real-world app you would find some way to throw them on a background thread.