我正在开发一个 android 应用程序,我试图在其中实现相机功能,而不是使用意图来避免获得 android 内置相机。
我正在使用 Preview.java 来显示相机在屏幕上看到的内容。
public void surfaceCreated(SurfaceHolder holder) {
Mydcamera = Camera.open();
try {
Mydcamera.setPreviewDisplay(holder);
Mydcamera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Preview.this.invalidate();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
但是每次我运行该应用程序时,我都会强制关闭下面的代码行
Mydcamera = Camera.open();
错误日志:
08-14 09:10:57.027: E/AndroidRuntime(2318): java.lang.RuntimeException: Fail to connect to camera service
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.hardware.Camera.native_setup(Native Method)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.hardware.Camera.<init>(Camera.java:294)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.hardware.Camera.open(Camera.java:271)
08-14 09:10:57.027: E/AndroidRuntime(2318): at com.ssn.newcamerademo.Preview.surfaceCreated(Preview.java:45)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.SurfaceView.updateWindow(SurfaceView.java:564)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.SurfaceView.updateWindow(SurfaceView.java:408)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:352)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.View.draw(View.java:6936)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.View.draw(View.java:6936)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.widget.FrameLayout.draw(FrameLayout.java:357)
08-14 09:10:57.027: E/AndroidRuntime(2318): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1904)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewRoot.draw(ViewRoot.java:1527)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewRoot.performTraversals(ViewRoot.java:1263)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.os.Looper.loop(Looper.java:130)
08-14 09:10:57.027: E/AndroidRuntime(2318): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-14 09:10:57.027: E/AndroidRuntime(2318): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 09:10:57.027: E/AndroidRuntime(2318): at java.lang.reflect.Method.invoke(Method.java:507)
08-14 09:10:57.027: E/AndroidRuntime(2318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-14 09:10:57.027: E/AndroidRuntime(2318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-14 09:10:57.027: E/AndroidRuntime(2318): at dalvik.system.NativeStart.main(Native Method)
不知道哪里出错了。请帮忙!谢谢!