I am working on an application using the camera. I want my application to have a custom menu that launches when the user presses the menu button.
Right now the application launches properly and the CameraPreview works fine. The menu opens correctly when the menu button is pressed. It even returns to the first activity from the menu when you press the button like it is supposed to.
The problem is that the CameraPreview no longer appears in the first activity when returning from the menu activity. What do I need to do so the CameraPreview will appear properly when returning to the first activity from the menu?
public class CameraMenuTest extends Activity implements Callback {
private Camera camera;
private MediaRecorder mediaRecorder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SurfaceView cameraPreview = (SurfaceView)findViewById(R.id.camera_preview);
SurfaceHolder holder = cameraPreview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
Intent mIntent = new Intent(CameraMenuTest.this, CameraMenuTestMenu.class);
startActivityForResult(mIntent, 0);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setOutputFile("/sdcard/myoutputfile.mp4");
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mediaRecorder == null) {
try {
camera = Camera.open();
camera.setPreviewDisplay(holder);
camera.startPreview();
}
catch (IOException e){
Log.d("CAMERA", e.getMessage());
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
}
}
public class CameraMenuTestMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Button btn = (Button)findViewById(R.id.btn);
btn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
finish();
return false;
}
});
}
}
I am assuming I need to do something in the onActivityResult() of the first activity.
I have tried:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
SurfaceView cameraPreview = (SurfaceView)findViewById(R.id.camera_preview);
SurfaceHolder holder = cameraPreview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
and also:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
camera = Camera.open();
camera.setPreviewDisplay(holder);
camera.startPreview();
}
Thank you for your help.
Most likely reason is that when you return to your preview activity and
surfaceCreatedis called again,mediaRecorderis notnull, thus the code that is supposed to start the preview is not executed.