我正在使用相机 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 不应该为空。我还在一个著名的 Android 应用示例 OCRTest中看到了 surfaceCreated 和 surfaceChanged 是空的, 所以我认为将 surfaceCreated 和 surfaceChanged 中的命令移动到 onResume 会更好也更可行。
我认为我的应用程序的第一个版本的流聊天应该是:主要活动 onCreate(创建 LinearLayout 及其surfaceView、相机按钮的地方)、调用surfaceCreated 和surfaceChanged、onResume。
所以我可以简单地将surfaceCreated 和surfaceChanged 中的命令移动到onResume。但它不起作用!我使用 Debug 发现表面为空。我认为没有调用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);
我随机设置了最后三个整数参数。当我启动 Activity 时,会弹出 Android 手机中的错误窗口。它说“引起:java.lang.RuntimeException:无法连接到相机服务”。但是怎么解决????非常感谢!
错误是:
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