我按照此页面上的指南对某项活动实施了拍照操作: https://developers.google.com/glass/develop/gdk/camera
我几乎已将该页面中的所有代码复制到我的活动中。图像捕获应该来自显示相机预览的我的活动。
唯一的变化是我正在处理相机快门按钮按下事件并在其上调用takePicture()。
我的代码如下:
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();
}
}
我在代码中添加了更多的调试日志,我注意到使用正确的requestCode输入了onActivityResult,但是resultCode = 0。
我所看到的是拍摄的照片,然后我可以看到“点击接受”屏幕(大概来自图像捕捉活动)。当我点击接受时,我可以听到敲击声,但没有任何反应。我可以多次点击,但仍然没有。然而,我可以向下滑动以停止图像捕获活动并恢复我的活动。