I am trying to call camera
instance, capture an image and return to the parent activity. I am able to create and call the camera
instance, capture and store the image, but the activity with the camera does not exit.
The Preview
activity creates a camera instance and implements methods like onPause()
, onResume()
and resetCam()
. The code is:
//capture button
buttonClick.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
camera.takePicture(shutterCallback, rawCallback, pngCallback);
}
}
protected void onResume() {
super.onResume();
int numCams = Camera.getNumberOfCameras();
if(numCams > 0){
try{
camera = Camera.open(0);
camera.startPreview();
preview.setCamera(camera);
} catch (RuntimeException ex){
Toast.makeText(ctx, getString(R.string.camera_not_found), Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onPause() {
if(camera != null) {
camera.stopPreview();
preview.setCamera(null);
camera.release();
camera = null;
}
super.onPause();
}
private void resetCam() {
camera.startPreview();
preview.setCamera(camera);
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
}
};
PictureCallback pngCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//save the iamge here
resetCam();
Log.d(TAG, "onPictureTaken - png");
}
};
After a single capture event, the control should be returned to parent activity PhotoCapture
which calls Preview
by:
Intent intent = new Intent(PhotoCapture.this,Preview.class);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
And performs onActivityResult()
for the same.
How should I release camera
instance for safe exit?
I want to return the path of image saved as well.
Thank you!
EDIT:
I tried to call finish(); this way:
buttonClick.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
camera.takePicture(shutterCallback, rawCallback, pngCallback);
Log.d("Tag","4");
finish();
}
});
I put the Log.d()
for getting the control flow:
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.d("Tag","1");
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Tag","2");
}
};
PictureCallback pngCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Tag","3");
//save image
resetCam();
Log.d(TAG, "onPictureTaken - png");
}
};
The log is:
02-19 14:00:28.046 4680-4680/preview.preview D/Tag﹕ 4
02-19 14:00:38.414 4680-4686/preview.preview W/Camera﹕ Camera server died!
There is no log for Tag:1 or Tag: 2 or Tag: 3!
So the activity is finished before the actual image is captured/stored.
Even though i set (result_code=OK) the image is not saved.
How can i resolve this?