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 8178323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:44:08+00:00 2026-06-06T23:44:08+00:00

I am trying to switch between the device’s Front and Back Camera while showing

  • 0

I am trying to switch between the device’s Front and Back Camera while showing the camera preview. I am following the sample provide by common ware. Below is the code which I am using. Whenever I click on the Flip button, the surface view goes black, and I don’t know where I am going wrong. I have tried to restart the current activity, but I don’t want like that.

package com.commonsware.android.camera;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;
public class PreviewDemo extends Activity {
    private SurfaceView preview = null;
    private SurfaceHolder previewHolder = null;
    private Camera camera = null;
    private boolean inPreview = false;
    private boolean cameraConfigured = false;
    private ToggleButton flipCamera;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    flipCamera = (ToggleButton) findViewById(R.id.flip);
    preview = (SurfaceView) findViewById(R.id.preview);
    previewHolder = preview.getHolder();
    previewHolder.addCallback(surfaceCallback);
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    flipCamera.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            restartPreview(isChecked);
        }
    });

}

@Override
public void onResume() {
    super.onResume();

    // camera=Camera.open();
    int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
    if (Camera.getNumberOfCameras() > 1
            && camId < Camera.getNumberOfCameras() - 1) {
        // startCamera(camId + 1);
        camera = Camera.open(camId + 1);
    } else {
        // startCamera(Camera.CameraInfo.CAMERA_FACING_BACK);
        camera = Camera.open(camId);
    }
    startPreview();
}

void restartPreview(boolean isFront) {
    if (inPreview) {
        camera.stopPreview();
    }
    //
    camera.release();

    // camera=null;
    // inPreview=false;
    // /*int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
    // if (Camera.getNumberOfCameras() > 1 && camId <
    // Camera.getNumberOfCameras() - 1) {
    // //startCamera(camId + 1);
    // camera = Camera.open(camId + 1);
    // } else {
    // //startCamera(Camera.CameraInfo.CAMERA_FACING_BACK);
    // camera = Camera.open(camId);
    // }*/
    int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
    if (isFront) {
        camera = Camera.open(camId);
        camera.startPreview();

    } else {
        camera = Camera.open(camId + 1);
        camera.startPreview();

    }
    // startPreview();
}

@Override
public void onPause() {
    if (inPreview) {
        camera.stopPreview();
    }

    camera.release();
    camera = null;
    inPreview = false;

    super.onPause();
}

private Camera.Size getBestPreviewSize(int width, int height,
        Camera.Parameters parameters) {
    Camera.Size result = null;

    for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
        if (size.width <= width && size.height <= height) {
            if (result == null) {
                result = size;
            } else {
                int resultArea = result.width * result.height;
                int newArea = size.width * size.height;

                if (newArea > resultArea) {
                    result = size;
                }
            }
        }
    }

    return (result);
}

private void initPreview(int width, int height) {
    if (camera != null && previewHolder.getSurface() != null) {
        try {
            camera.setPreviewDisplay(previewHolder);
        } catch (Throwable t) {
            Log.e("PreviewDemo-surfaceCallback",
                    "Exception in setPreviewDisplay()", t);
            Toast.makeText(PreviewDemo.this, t.getMessage(),
                    Toast.LENGTH_LONG).show();
        }

        if (!cameraConfigured) {
            Camera.Parameters parameters = camera.getParameters();
            Camera.Size size = getBestPreviewSize(width, height, parameters);

            if (size != null) {
                parameters.setPreviewSize(size.width, size.height);
                camera.setParameters(parameters);
                cameraConfigured = true;
            }
        }
    }
}

private void startPreview() {
    if (cameraConfigured && camera != null) {

        camera.startPreview();
        inPreview = true;
    }
}

SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
    public void surfaceCreated(SurfaceHolder holder) {
        // no-op -- wait until surfaceChanged()
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        initPreview(width, height);
        startPreview();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // no-op
        if (camera != null) {
            /*
             * Call stopPreview() to stop updating the preview surface.
             */
            camera.stopPreview();

            /*
             * Important: Call release() to release the camera for use by
             * other applications. Applications should release the camera
             * immediately in onPause() (and re-open() it in onResume()).
             */
            camera.release();

            camera = null;
        }
    }
};
}
  • 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-06-06T23:44:09+00:00Added an answer on June 6, 2026 at 11:44 pm

    You seem to have forgotten to call setPreviewDisplay() (or, in your case, initPreview()) before calling startPreview() from your restartPreviev() method. Effectively, you’re trying to start preview without specifying a surface to render the preview into.

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

Sidebar

Related Questions

I am trying to provide an option to switch between webcams used to display
I'm trying to use jquery to switch between three images once you click on
I'm trying to add an effect to a switch between 2 views. The switch
I'm trying to use GMFBridge to switch between multiple stream buffer graphs and I
I am trying to switch between external and internal build via some export variable
I'm trying to switch between the UIReturnKeyGo and the UIReturnKeyNext buttons depending on whether
I'm trying to seamlessly switch between two independent HttpListener instances on the same address.
I am trying to use Slow Cheetah to switch between a local db connection
I'm trying to use a footer navbar as switch between free and busy status.
I'm trying to write some code that allows me to switch between SQLCE (locally

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.