I am following the guide on this page to implement a photo taking action on an activity: https://developers.google.com/glass/develop/gdk/camera
I have pretty much copied all the code from that page into my activity. The image capture should happen from my activity which is displaying the camera preview.
The only change is that I am handling the camera shutter button pressed event and calling takePicture() on it.
The code I have is below:
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
Log.e("CAMERA", "photo ready");
String picturePath = data.getStringExtra(
CameraManager.EXTRA_PICTURE_FILE_PATH);
processPictureWhenReady(picturePath);
}
super.onActivityResult(requestCode, resultCode, data);
}
private void processPictureWhenReady(final String picturePath) {
final File pictureFile = new File(picturePath);
if (pictureFile.exists()) {
// The picture is ready; process it.
Log.e("CAMERA", "photo ready2");
} else {
// The file does not exist yet. Before starting the file observer, you
// can update your UI to let the user know that the application is
// waiting for the picture (for example, by displaying the thumbnail
// image and a progress indicator).
Log.e("CAMERA", "photo ready3");
final File parentDirectory = pictureFile.getParentFile();
FileObserver observer = new FileObserver(parentDirectory.getPath(),
FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) {
// Protect against additional pending events after CLOSE_WRITE
// or MOVED_TO is handled.
private boolean isFileWritten;
@Override
public void onEvent(int event, String path) {
if (!isFileWritten) {
// For safety, make sure that the file that was created in
// the directory is actually the one that we're expecting.
File affectedFile = new File(parentDirectory, path);
isFileWritten = affectedFile.equals(pictureFile);
if (isFileWritten) {
stopWatching();
// Now that the file is ready, recursively call
// processPictureWhenReady again (on the UI thread).
runOnUiThread(new Runnable() {
@Override
public void run() {
processPictureWhenReady(picturePath);
}
});
}
}
}
};
observer.startWatching();
}
}
I added more debugging logs into the code and I noticed that onActivityResult is getting entered with the correct requestCode but resultCode = 0.
What I am seeing is the photo being taken, then I am can see the "Tap to accept" screen (presumably from the image capture activity). When I tap to accept I can hear the tap noise but nothing happens. I can tap multiple times and still nothing. I can however swipe down to stop the image capture activity and resume my activity.