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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:23:27+00:00 2026-05-13T00:23:27+00:00

I have created a CapturePreview class and CameraManager class the following way: CapturePreview: public

  • 0

I have created a CapturePreview class and CameraManager class the following way:

CapturePreview:

public class CaptureView extends SurfaceView implements Callback{

private final SurfaceHolder surfaceHolder;
FileReaderWriter fileRW;
int frameCount;

private static final int MSG_FRAME_REFRESHED = 1;
private static final int MSG_AUTOFOCUS = 2;

private final CameraManager mCameraManager;

private boolean mRecording;

public CaptureView(Context context, AttributeSet attrs)
{
    super(context, attrs);

    surfaceHolder = getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    CameraManager.init(context);
    mCameraManager = CameraManager.get();

    init(context);
}

public CaptureView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);

    surfaceHolder = getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surfaceHolder.setSizeFromLayout();

    CameraManager.init(context);
    mCameraManager = CameraManager.get();

    init(context);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

    mCameraManager.startPreview();

    //mCameraManager.requestPreviewFrame(mCameraHandler, MSG_FRAME_REFRESHED);
    mCameraManager.requestAutoFocus(mCameraHandler, MSG_AUTOFOCUS);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    try
    {
        mCameraManager.openDriver(surfaceHolder);
    }
    catch(Exception e)
    {
        //TODO: display error
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mCameraManager.stopPreview();
}

private void init(Context context)
{
    setFocusable(true);
    mRecording = false;
    fileRW = new FileReaderWriter();
    frameCount = 0;
}



public void setRecording(boolean isRecording) {
    this.mRecording = isRecording;
}

public boolean isRecording() {
    return mRecording;
}

private Handler mCameraHandler = new CameraHandler();

private class CameraHandler extends Handler
{
    @Override
    public void handleMessage(Message msg)
    {
        switch(msg.what)
        {
        case MSG_FRAME_REFRESHED:
//              String path = "JPGFrame" + frameCount;
//              fileRW.setPath(path);
//              fileRW.WriteToFile((byte[]) msg.obj);
//              frameCount++;

            break;
        }
    }
}

}

CameraManager:

public final class CameraManager {

@Override
protected void finalize() throws Throwable {
    closeDriver();
    super.finalize();
}

private static final String TAG = "CameraManager";

private static CameraManager mCameraManager;
private Camera mCamera;
private final Context mContext;
private Point mScreenResolution;
private Rect mFramingRect;
private Handler mPreviewHandler;
private int mPreviewMessage;
private Handler mAutoFocusHandler;
private int mAutoFocusMessage;
private boolean mPreviewing;

public static synchronized void init(Context context) {
    if (mCameraManager == null) {
      mCameraManager = new CameraManager(context);
      mCameraManager.getScreenResolution();
    }
  }

  public static CameraManager get() {
    return mCameraManager;
  }

  private CameraManager(Context context) {
    mContext = context;
    mCamera = null;
    mPreviewing = false;
  }

  public void openDriver(SurfaceHolder holder) throws IOException {
    // "throws IOException added to accommodate Android 1.5
    if (mCamera == null) {
      mCamera = Camera.open();
      setCameraParameters();
      mCamera.setPreviewDisplay(holder);
    }
  }

  public void closeDriver() {
    if (mCamera != null) {
      mCamera.release();
      mCamera = null;
    }
  }

  public void startPreview() {
    if (mCamera != null && !mPreviewing) {
      mCamera.startPreview();
      mPreviewing = true;
    }
  }

  public void stopPreview() {
    if (mCamera != null && mPreviewing) {
      mCamera.setPreviewCallback(null);
      mCamera.stopPreview();
      mPreviewHandler = null;
      mAutoFocusHandler = null;
      mPreviewing = false;
    }
  }


  public void requestPreviewFrame(Handler handler, int message) {
    if (mCamera != null && mPreviewing) {
      mPreviewHandler = handler;
      mPreviewMessage = message;
      mCamera.setPreviewCallback(previewCallback);
    }
  }

  public void requestAutoFocus(Handler handler, int message) {
    if (mCamera != null && mPreviewing) {
      mAutoFocusHandler = handler;
      mAutoFocusMessage = message;
      mCamera.autoFocus(autoFocusCallback);
    }
  }


  public Rect getFramingRect() {
    if (mFramingRect == null) {
      int size = ((mScreenResolution.x < mScreenResolution.y) ? mScreenResolution.x :
          mScreenResolution.y) * 3 / 4;
      int leftOffset = (mScreenResolution.x - size) / 2;
      int topOffset = (mScreenResolution.y - size) / 2;
      mFramingRect = new Rect(leftOffset, topOffset, leftOffset + size, topOffset + size);
    }
    return mFramingRect;
  }


  PreviewCallback previewCallback = new PreviewCallback()
  {

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        if(mPreviewHandler != null)
        {
            Message message = mPreviewHandler.obtainMessage(mPreviewMessage,
                mScreenResolution.x, mScreenResolution.y, data);
                message.sendToTarget();
        }
    } 
  };

  AutoFocusCallback autoFocusCallback = new AutoFocusCallback()
  {

    @Override
    public void onAutoFocus(boolean success, Camera camera) {
        if(mAutoFocusHandler != null)
        {
            Message message = mAutoFocusHandler.obtainMessage(mAutoFocusMessage, success);
            message.sendToTarget();
        }   
    }  
  };

  private void setCameraParameters() {
    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setPreviewSize(mScreenResolution.x, mScreenResolution.y);
    parameters.setPictureFormat(PixelFormat.JPEG);
    parameters.setPreviewFormat(PixelFormat.JPEG);
    mCamera.setParameters(parameters);
  }

  private Point getScreenResolution() {
    if (mScreenResolution == null) {
      WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
      Display display = wm.getDefaultDisplay();
      mScreenResolution = new Point(display.getWidth(), display.getHeight());
    }
    return mScreenResolution;
  }

}

The video preview works fine this way until the sub-activity starts in the main activity. When sub-activity starts the CaptureView.surfaceDestroyed(SurfaceHolder holder) is called and the video preview stops. Then when the sub-activity closes the CaptureView.surfaceCreated(SurfaceHolder holder) is executed, but the video preview doesn’t start.

Does anyone know how to fix the problem in order to successfully restart video preview after surfaceDestroyed(SurfaceHolder holder) is executed?

Thanks!

  • 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-13T00:23:28+00:00Added an answer on May 13, 2026 at 12:23 am

    You must release the camera at ‘surfaceDestroyed’ seeing as you are trying to acquire access to it again in ‘surfaceCreated’, which would throw you an exception if the camera is already acquired.

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

Sidebar

Related Questions

I have created a list view with a custom baseAdapter (my own class extends
I have created the following string field for indexing. <fieldType name=short_text_for_sort class=solr.StrField omitNorms=true sortMissingLast=true
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created two imageViews promatically as shown below: public void createImageViews(Integer count){ ImageView[]
I have created RadioGroup and RadioButton dynamically as following: RadioGroup radioGroup = new RadioGroup(context);
I have created the following Map Reduce and came across something curious. I'm counting
i have created one jsp file but it doesn't running in any way..!! it
I have created a SeedData class, and populated it with the values that I
I have created one class that directly map to ConfigSection of web. config. My
I have created new environment staging. Locally it works. I run it this way:

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.