我正在使用 android SurfaceView 在我的活动中显示相机预览。
在移动到正在播放任何视频的任何其他应用程序并返回时,预览是空白的。我正在使用安卓 2.3.3。当我打开 logcat 时,它显示以下内容
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)
我使用的代码如下:
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");
}
( https://groups.google.com/forum/?fromgroups=#!topic/android-developers/ylIgXIKKGo8 )报告了同样的问题。但没有提供解决方案。