Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5947783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:00:48+00:00 2026-05-22T17:00:48+00:00

I am working on an application using the camera. I want my application to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:00:49+00:00Added an answer on May 22, 2026 at 5:00 pm

    Most likely reason is that when you return to your preview activity and surfaceCreated is called again, mediaRecorder is not null, thus the code that is supposed to start the preview is not executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on web application using VB in ASP.NET. Right now I have
I have a working application using Json.NET (newtonsoft) as a custom serializer. Currently I'm
I'm working on an application that requires arabic text to be displayed, using custom
I am working on window application using c# . i want to show my
Greetings, I'm working in windows application using C#. I have typed-dataset called packetsDBDataSet and
I'm working on an application using OpenGL and C++ that parses some structured input
I have been working on an application using GAE in eclipse and I have
I have a working application using Three20. It basically is a bunch of nested
I am working on win application using c#, I want to send Ctrl +
Hi I am working Android application development using titanium studio.I have developed small application.my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.