我正在使用Android SurfaceView在我的活动中显示相机预览。
在移动到播放任何视频的任何其他应用程序并返回时,预览为空白。我正在使用android 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)报告了同样的问题。但没有提供解决方案。
答案 0 :(得分:0)
mCamera.lock();
mCamera.release();
这两个功能不是这样使用你必须像这样使用它们
mCamera.lock();
... your camera setting code etc or any operation goes here
mCamera.release();
这是使用camera
的表面视图的示例答案 1 :(得分:0)
您没有在surfaceChanged函数中调用setPreviewDisplay();这意味着您可能无法在返回应用程序时正确设置显示器。
在调用startPreview之前,尝试将setPreviewDisplay()调用移动到surfaceChanged中;你可以忽略surfaceCreated。
在release()之前,你也不需要在你的相机实例上调用lock(),尽管它也没有任何有害影响。