我正在使用相机API编写一个应用程序:显示预览并拍照。第一个版本效果非常好。在Preview类中,继承自SurfaceView并实现了SurfaceHolder.Callback接口。
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
hasSurface = true;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// It will be called immediately after surfaceCreated
// I move it to Resume.
setCameraPreviewParameters();
camera.startPreview();
}
在主活动中:
public class CameraDemo extends Activity
我还设置了一个成员变量: CameraUnit ui; 它是:
public class CameraUnit extends LinearLayout
在onCreate方法中,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ui = new CameraUnit(this);
setContentView((View) ui);
Log.d(TAG, "onCreate'd");
}
它创建了一个LinearLayout对象,其中包含SurfaceView和相机按钮。
onResume方法是:
@Override
protected void onResume() {
super.onResume();
}
虽然它可以工作,但我认为onResume不应该为空。我还看到surfaceCreated和surfaceChanged在著名的Android应用程序示例OCRTest中为空。因此,我认为将命令从surfaceCreated和surfaceChanged移动到onResume更好且可行。
我认为第一个版本的应用程序的流程图应该是: 主活动onCreate(创建LinearLayout及其surfaceView、相机按钮), 调用surfaceCreated和surfaceChanged, onResume。
因此,我可以简单地将surfaceCreated和surfaceChanged中的命令移动到onResume中。 但它不起作用! 我使用调试找出表面为空。我认为surfaceCreatd没有被调用。我的应用程序与OCRTest不同,在主活动中实现了SurfaceHolder.Callback。我在SurfaceView中实现了SurfaceHolder.Callback:
class Preview extends SurfaceView implements SurfaceHolder.Callback
所以我在onResume中添加了另外两行代码。
ui.preview.surfaceCreated(ui.preview.mHolder);
ui.preview.surfaceChanged(ui.preview.mHolder, 0, 800, 400);
我随机设置了最后三个整数参数。当我启动活动时,Android手机上弹出错误窗口。 它说“Caused by: java.lang.RuntimeException: Fail to connect to camera service”。 但是如何修复它?非常感谢!
错误信息如下:
01-07 00:27:57.173: W/dalvikvm(11625): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
01-07 00:27:57.173: E/AndroidRuntime(11625): FATAL EXCEPTION: main
01-07 00:27:57.173: E/AndroidRuntime(11625): java.lang.RuntimeException: Unable to resume activity {com.example/com.example.CameraDemo}: java.lang.RuntimeException: Fail to connect to camera service
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2460)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2481)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1847)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.os.Looper.loop(Looper.java:150)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.app.ActivityThread.main(ActivityThread.java:4263)
01-07 00:27:57.173: E/AndroidRuntime(11625): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 00:27:57.173: E/AndroidRuntime(11625): at java.lang.reflect.Method.invoke(Method.java:507)
01-07 00:27:57.173: E/AndroidRuntime(11625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 00:27:57.173: E/AndroidRuntime(11625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 00:27:57.173: E/AndroidRuntime(11625): at dalvik.system.NativeStart.main(Native Method)
01-07 00:27:57.173: E/AndroidRuntime(11625): Caused by: java.lang.RuntimeException: Fail to connect to camera service
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.hardware.Camera.native_setup(Native Method)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.hardware.Camera.<init>(Camera.java:265)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.hardware.Camera.open(Camera.java:241)
01-07 00:27:57.173: E/AndroidRuntime(11625): at com.example.Preview.surfaceCreated(Preview.java:60)
01-07 00:27:57.173: E/AndroidRuntime(11625): at com.example.CameraDemo.onResume(CameraDemo.java:64)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1242)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.app.Activity.performResume(Activity.java:3904)
01-07 00:27:57.173: E/AndroidRuntime(11625): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2450)
01-07 00:27:57.173: E/AndroidRuntime(11625): ... 12 more
01-07 00:27:59.455: I/Process(11625): Sending signal. PID: 11625 SIG: 9