0

I am using android SurfaceView to show camera preview in my activity.

On moving to any other application which is playing any video, and on returning back, the preview is blank. I am using android 2.3.3. When I open the logcat it shows the following

E/SEC_Overlay(  107): Error - overlays already in use
W/CameraService(   76): Overlay create failed - retrying
E/SEC_Overlay(  107): Error - overlays already in use
W/CameraService(   76): Overlay create failed - retrying
E/SEC_Overlay(  107): Error - overlays already in use
W/CameraService(   76): Overlay create failed - retrying
E/SEC_Overlay(  107): Error - overlays already in use
W/CameraService(   76): Overlay create failed - retrying
E/SEC_Overlay(  107): Error - overlays already in use
...
E/SEC_Overlay(  107): Error - overlays already in use
W/CameraService(   76): Overlay create failed - retrying
E/SEC_Overlay(  107): Error - overlays already in use
W/CameraService(   76): Overlay create failed - retrying
E/CameraService(   76): Overlay Creation Failed!
D/AndroidRuntime( 1618): Shutting down VM
W/dalvikvm( 1618): threadid=1: thread exiting with uncaught exception (group=0x40015560)

E/AndroidRuntime( 1618): FATAL EXCEPTION: main
E/AndroidRuntime( 1618): java.lang.RuntimeException: startPreview failed
E/AndroidRuntime( 1618):    at android.hardware.Camera.startPreview(Native Method)
E/AndroidRuntime( 1618):    at com.android.myapp.MyActivity.startCapturing(MyActivity.java:216)
E/AndroidRuntime( 1618):    at com.android.myapp.MyActivity.surfaceChanged(MyActivity.java:328)
E/AndroidRuntime( 1618):    at android.view.SurfaceView.updateWindow(SurfaceView.java:549)
E/AndroidRuntime( 1618):    at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
E/AndroidRuntime( 1618):    at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
E/AndroidRuntime( 1618):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
E/AndroidRuntime( 1618):    at android.view.View.draw(View.java:6883)
E/AndroidRuntime( 1618):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
...
E/AndroidRuntime( 1618):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
E/AndroidRuntime( 1618):    at android.view.View.draw(View.java:6883)
E/AndroidRuntime( 1618):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
E/AndroidRuntime( 1618):    at     com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
E/AndroidRuntime( 1618):    at android.view.ViewRoot.draw(ViewRoot.java:1522)
E/AndroidRuntime( 1618):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
E/AndroidRuntime( 1618):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
E/AndroidRuntime( 1618):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1618):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 1618):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 1618):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1618):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1618):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 1618):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 1618):    at dalvik.system.NativeStart.main(Native Method)

The code I have used is given below:

    Camera mCamera;
    SurfaceView mPreview;

    @Override
protected void onResume() {
    // TODO Auto-generated method stub      
    try{
        if(mCamera!=null){
            mCamera.lock();
            mCamera.release();
            mCamera=null;
    }

    mPreview=((SurfaceView) findViewById(R.id.camera_preview));         
        mPreview.getHolder().addCallback(this);
        mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);          
    }catch(Exception e){
       e.printStackTrace();
    }

    try{
        mCamera = Camera.open();
    }catch(Exception e){
      e.printStackTrace();          
    }

    super.onResume();
}


    @Override
protected void onPause() {
    // TODO Auto-generated method stub      
    super.onPause();
    try{
        if(mCamera == null){
        return;
    }
    mCamera.lock();
    mCamera.release();
    mCamera = null;
    }catch(Exception e){
            e.printStackTrace();
    }       

}

    @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    try{
        if(mCamera!=null){              
            Camera.Parameters params = mCamera.getParameters();         
            List<Camera.Size> sizes = params.getSupportedPreviewSizes();
            Camera.Size selected = sizes.get(0);
            params.setPreviewSize(selected.width,selected.height);
            mCamera.setParameters(params);          
            mCamera.setDisplayOrientation(90);              
            mCamera.startPreview(); 
            Log.v("SURF CHANGED","changed");                
        }
    }catch (Exception e) {
        e.printStackTrace();            
    }
}

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(mPreview.getHolder());
            Log.v("SURF CREATED","created");                
        } catch (Exception e) {
            e.printStackTrace();               
        }
    }

@Override
public void surfaceDestroyed(SurfaceHolder holder) {        
    Log.i("PREVIEW","surfaceDestroyed");
}

The same problem has been reported at (https://groups.google.com/forum/?fromgroups=#!topic/android-developers/ylIgXIKKGo8). But solution was not provided.

DSP
  • 487
  • 2
  • 5
  • 18

2 Answers2

0
 mCamera.lock();
 mCamera.release();

these two function are not use like this you have to use them like this

 mCamera.lock();
 ... your camera setting code etc or any operation goes here

 mCamera.release();

here is the an example of using surface view with camera

Community
  • 1
  • 1
Ali Imran
  • 8,927
  • 3
  • 39
  • 50
0

You are not calling setPreviewDisplay() in your surfaceChanged function; this means you might not be setting your display correctly when returning to your application.

Try moving the setPreviewDisplay() call into surfaceChanged, before you call startPreview; you can ignore surfaceCreated.

You also don't need to call lock() on your camera instance before release(), although it has no harmful effects either.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
  • I don't think it the problem is with setPreviewDisplay() because, if I go to some other activity and come back to my activity, the preview is being shown. The error is coming when i move to some activity which is playing video. The logcat shows **overlay create failed**. – DSP Mar 04 '13 at 08:38