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

  • Home
  • SEARCH
  • 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 6539449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:51:38+00:00 2026-05-25T10:51:38+00:00

I have a surfaceview which is displaying a camera preview I have it taking

  • 0

I have a surfaceview which is displaying a camera preview I have it taking a picture and display it on the surfaceview but when I press the back button it closes the application but I want it to display the original camera preview.

I also only want to reset the display when the picture is being displayed and have normally functionality returned to back button when camera preview is showing.

public class cameraView extends Activity implements SurfaceHolder.Callback{
 private SurfaceView preview=null;
 private SurfaceHolder previewHolder=null;
 private Camera camera=null;
 private ImageButton bt = null;
 private Toast t = null;
 private Camera.Parameters param = null; 
 private Button b = null;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.cam);

  t = Toast.makeText(this, "Just Click The Magnifying Glass To Search", 5000);//creates a new pop up message that lasts for 5 seconds

  t.setGravity(Gravity.CENTER|Gravity.CENTER,   0, 0);
     t.show();




 b = (Button)findViewById(R.id.test);
 b.setOnClickListener(search);
 bt = (ImageButton)findViewById(R.id.button);//creates instance of button
 bt.setOnClickListener(search);//starts an on click listener for button



  preview=(SurfaceView)findViewById(R.id.myview);//creates instance of surfaceview
  previewHolder=preview.getHolder();//creates a surfaceholder
  previewHolder.addCallback(this);//sets surfaceholder callback as the activity
  previewHolder.setType(3);//sets the type to SURFACE_TYPE_PUSH_BUFFERS 

}

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { //creates a method that is called automatically when the surface is changed
        // TODO Auto-generated method stub
        Camera.Parameters param = camera.getParameters();//sets param to be equal to camera parametors
        param.setPreviewSize(width, height);//sets width and height to that of what is passed back to it when callback calls it 
        //param.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(param);//sets the camera parameters to param
        camera.startPreview();//starts the preview
        camera.autoFocus(cb);//calls autofocus callback method

    }

    public void surfaceCreated(SurfaceHolder holder) {//called when the surface has been created
        // TODO Auto-generated method stub
        camera = Camera.open();//opens the camera and sets it to the camera variable




        try{
            camera.setPreviewDisplay(previewHolder);//sets the display area to previewHolder


        }catch(Throwable t){
            Log.e(""+t, null);
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {//called when sureface is destroyed or when activity is closed
        // TODO Auto-generated method stub
        camera.stopPreview();//stops the preview
        camera.release();//releases the camera
        camera = null;// clears the camera so it contains no information 
    }





    AutoFocusCallback cb = new AutoFocusCallback(){
    public void onAutoFocus(boolean success, Camera camera) {
        // TODO Auto-generated method stub
        //return true;
    }

    };




    private OnClickListener search = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked
            switch(v.getId()){
            case R.id.test:
                param = camera.getParameters();
            if(param.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)){
                /*
                 * IF statement to check the current flash mode and change it appropriately
                 */
                param.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(param); 
            }else{
            param.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(param);
            }

            break;

            case R.id.button:
                camera.autoFocus(cb);//calls autofocus with call back of cb
                  Handler handler = new Handler(); 
                    handler.postDelayed(new Runnable() { 
                         public void run() { 

                             camera.takePicture(null, mPictureCallback, mPictureCallback);

                         } 
                    }, 2000);           
                     break; 

                }

            }

    };





    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] imageData, Camera c) {

            bt.setVisibility(4);
            b.setVisibility(4);
            b.setEnabled(false);
            bt.setEnabled(false);



        }
    };

above is my entire code (minus the imports) of my activity.

any help is greatly appreciated.

  • 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-25T10:51:39+00:00Added an answer on May 25, 2026 at 10:51 am

    What you need to do is override the onBackPressed() method:

    @Override public void onBackPressed()
    {    
      // Restart camera
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that I have taking a picture, and then it is
I have recently built an application that shows you a camera preview on screen.
I am trying to display the camera preview with an image overlay and have
In my application I have many classes and activities. Droid is a class which
I have a dialog box which appears on top of a SurfaceView. I would
I have two Activities, both have an inner class SurfaceView, which has an inner
I have an RGB565 byte array which I display on my Canvas like this:
I have a SurfaceView which is currently drawing a series of Bitmaps. These bitmaps
I have a game which has surfaceview and running on a thread. if I
I have two button's in the main menu. I invoking the camera when I

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.