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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:17:22+00:00 2026-06-15T09:17:22+00:00

I am creating a camera app but I have problems on startPreview , it

  • 0

I am creating a camera app but I have problems on startPreview, it sends me:

java.lang.RuntimeException: startPreview failed

here is my camera Activity:

public class CameraActivity extends Activity {

private Camera mCamera;
private CameraPreview  mPreview;
private Target_Frame targetFrame;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_layout);




    mCamera=getCameraInstance();
    mPreview=new CameraPreview(this, mCamera);




    FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);
    preview.addView(mPreview);

}




/** Check if this device has a camera only if not specified in the manifest */
public boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

/**Check if the device has flash*/
public boolean checkFlash(Context context){
    if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
        //the device has flash
        return true;
    }else{
        //no flash
        return false;
    }

}




@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    releaseCamera();
}




@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    releaseCamera();
}




@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    //Test if i have to put all this code like in onCreate
    if(mCamera!=null){
        return;
    }
    mCamera=getCameraInstance();

    if(mPreview!=null){
        return;
    }
    mPreview=new CameraPreview(this, mCamera);
    FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);
    preview.addView(mPreview);


}


private void releaseCamera(){
    if (mCamera != null){
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}}

And here is my SurfaceView code:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "CameraPreview";
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();

        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here
        Parameters parameters= mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

And here is my error log:

12-01 13:17:01.135: E/AndroidRuntime(1161): FATAL EXCEPTION: main

12-01 13:17:01.135: E/AndroidRuntime(1161): java.lang.RuntimeException: startPreview

12-01 13:17:01.135: E/AndroidRuntime(1161):     at com.example.prueba.CameraPreview.surfaceCreated(CameraPreview.java:36) 
  • 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-15T09:17:22+00:00Added an answer on June 15, 2026 at 9:17 am

    I have solved deleting some lines in surfaceChanged

     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            // If your preview can change or rotate, take care of those events here.
            // Make sure to stop the preview before resizing or reformatting it.
            Log.d("Function", "surfaceChanged iniciado");
            if (mHolder.getSurface() == null){
              // preview surface does not exist
              return;
            }
    
            // stop preview before making changes
            try {
                mCamera.stopPreview();
            } catch (Exception e){
              // ignore: tried to stop a non-existent preview
            }
    
            // set preview size and make any resize, rotate or
            // reformatting changes here
    
    
            // start preview with new settings
            try {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
    
            } catch (Exception e){
                Log.d(TAG, "Error starting camera preview: " + e.getMessage());
            }
        }
    

    So the error must be in i one of these lines:

    Parameters parameters= mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
    

    Someone could explain me what was wrong in those lines?

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

Sidebar

Related Questions

I'm having problems creating a 3D first-person camera in DirectX 11. I have a
In my app, I have an Activity that acts as a camera preview. I
I am creating an application that is similar to the camera app provided in
I have to develop an android app for which I have to use camera
I am currently creating a portion of my iPhone app to basically have a
I'm creating a SurfaceView for my camera preview and attempting to have it occupy
I'm creating an app for the iPhone which is supposed to start in camera
I am creating a live streaming app, but I'm stuck at a certain point.
I have an application that uses camera to take images. Application works but is
Im creating an Air/flex app that captures multiple images thru a camera. now that

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.