我一直试图在使用相机实时预览作为背景的特定条件之后调用同一个类。但是我收到了一个错误。
logcat的:
02-03 19:52:42.274: E/AndroidRuntime(454): FATAL EXCEPTION: main
02-03 19:52:42.274: E/AndroidRuntime(454): java.lang.RuntimeException: Fail to connect to camera service
02-03 19:52:42.274: E/AndroidRuntime(454): at android.hardware.Camera.native_setup(Native Method)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.hardware.Camera.<init>(Camera.java:258)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.hardware.Camera.open(Camera.java:235)
02-03 19:52:42.274: E/AndroidRuntime(454): at com.example.gems.GamePlay.surfaceCreated(GamePlay.java:996)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.View.draw(View.java:6883)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.View.draw(View.java:6883)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-03 19:52:42.274: E/AndroidRuntime(454): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.ViewRoot.draw(ViewRoot.java:1522)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.os.Looper.loop(Looper.java:123)
02-03 19:52:42.274: E/AndroidRuntime(454): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-03 19:52:42.274: E/AndroidRuntime(454): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 19:52:42.274: E/AndroidRuntime(454): at java.lang.reflect.Method.invoke(Method.java:507)
02-03 19:52:42.274: E/AndroidRuntime(454): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-03 19:52:42.274: E/AndroidRuntime(454): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-03 19:52:42.274: E/AndroidRuntime(454): at dalvik.system.NativeStart.main(Native Method)
我正在创建这样的相机。
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
// TODO Auto-generated method stub
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
Parameters parameters = camera.getParameters();
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
parameters.setPreviewSize(height, width);
camera.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
parameters.setPreviewSize(width, height);
}
if(display.getRotation() == Surface.ROTATION_180)
{
parameters.setPreviewSize(height, width);
}
if(display.getRotation() == Surface.ROTATION_270)
{
parameters.setPreviewSize(width, height);
camera.setDisplayOrientation(180);
}
camera.setParameters(parameters);
try
{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
}
catch(Exception e)
{
Log.d("Error", "Cannot start preview", e);
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
camera = Camera.open();
camera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) { }
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if (null == camera)
return;
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
当我尝试调用完成然后再次调用同一个类时,我在顶部收到错误。有什么想法吗?
答案 0 :(得分:0)
当您调用Camera.open时,您将保留相机,直到您调用Camera.release()。 这里奇怪的是你在surfaceDestroyed中调用camera.release。所以应该释放相机。
我建议你做的是在调用finish()之前调用camera.release。 你也可以在surfaceDestroyed中的camera.stopPreview()行上放一个断点。