2011-04-13 86 views
0

嘿傢伙 我正在通過我的應用程序訪問攝像頭。 這是一切順利,我甚至可以保存一張照片到我的SD卡,但必須重新啓動手機才能使攝像頭正常工作

當我捕捉圖像的應用程序似乎得到斯塔克加如果我去家裏和書房再次返回到應用程序我有一個力量關閉錯誤。以下是錯誤我在logcat的

04-13 17:20:40.818: ERROR/AndroidRuntime(1149): java.lang.RuntimeException: Fail to connect to camera service 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.hardware.Camera.native_setup(Native Method) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.hardware.Camera.<init>(Camera.java:118) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.hardware.Camera.open(Camera.java:91) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at org.mycamera.mycamera.surfaceCreated(mycamera.java:70) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.SurfaceView.updateWindow(SurfaceView.java:540) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.View.draw(View.java:6742) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewGroup.drawChild(ViewGroup.java:1648) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.View.draw(View.java:6742) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.widget.FrameLayout.draw(FrameLayout.java:352) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewRoot.draw(ViewRoot.java:1422) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1167) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1744) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.os.Looper.loop(Looper.java:144) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at android.app.ActivityThread.main(ActivityThread.java:4937) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at java.lang.reflect.Method.invoke(Method.java:521) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):  at dalvik.system.NativeStart.main(Native Method) 

下面得到的是代碼: -

SurfaceHolder holder; 
SurfaceView surface; 
Camera camera; 
Boolean isPreviewRunning; 
//Preview mpreview; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    isPreviewRunning = false; 
    surface = (SurfaceView)findViewById(R.id.surface); 
    holder = surface.getHolder(); 
    holder.addCallback(this); 
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    Button btn = (Button)findViewById(R.id.click); 
    btn.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg); 
      //onCreate(null); 
     } 

    }); 

} 


@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
/* if (isPreviewRunning) { 
      camera.stopPreview(); 
     } */ 

    Camera.Parameters parameters = camera.getParameters(); 

    List<Size> sizes = parameters.getSupportedPreviewSizes(); 
    Size size = sizes.get(0); 
    parameters.setPreviewSize(size.width, size.height); 

    camera.setParameters(parameters); 
    camera.startPreview(); 
    isPreviewRunning=true; 
} 
@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    // TODO Auto-generated method stub 
    camera = Camera.open(); 
    try { 
     camera.setPreviewDisplay(holder); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    // TODO Auto-generated method stub 
    camera.stopPreview(); 
    camera.release(); 
    camera=null; 
} 

protected void onRestoreInstanceState(Bundle savedInstanceState) 
{ 

super.onRestoreInstanceState(savedInstanceState); 
} 

Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() { 
public void onPictureTaken(byte[] data, Camera c) { 
    Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data); 
    camera.startPreview(); 
} 
}; 

Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() { 
public void onPictureTaken(byte[] data, Camera c) { 
    Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data); 
} 
}; 

Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() { 
public void onShutter() { 
Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK"); 
} 
}; 

protected void onResume() 
{ 

Log.e(getClass().getSimpleName(), "onResume"); 
super.onResume(); 
} 

protected void onSaveInstanceState(Bundle outState) 
{ 
super.onSaveInstanceState(outState); 
} 

protected void onStop() 
{ 
Log.e(getClass().getSimpleName(), "onStop"); 
super.onStop(); 
} 
+0

看看你是否可以從http://stackoverflow.com/questions/3001179/cannot-run-android-camera-preview-sample – rajath 2011-04-13 12:28:14

回答

2

最有可能的,這是因爲你的再次調用startPreview()爲時尚早。從Camera.takePicture()上的SDK文檔:

調用此方法後,您不得調用startPreview()或拍攝其他圖片,直到JPEG回調已返回。

要調用startPreview()重新啓動您的mPictureCallbackRaw預覽框,當你應該等到mPictureCallbackJpeg。這可能與Camera服務混淆並將其置於奇怪的狀態。

希望有助於!

相關問題