gpt4 book ai didi

Android 相机 API 应用程序在 onResume 时崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:33:03 27 4
gpt4 key购买 nike

我正在使用相机 API 编写一个应用程序:显示预览并拍照。第一个版本效果很好。在类 Preview extends SurfaceView implements 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();

}

在主要 Activity 中:

public class CameraDemo extends Activity

我还设置了一个成员:相机单元用户界面;它是:

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 在 famous Android app example OCRTest 中为空所以我认为将 surfaceCreated 和 surfaceChanged 中的命令移动到 onResume 会更好,也是可行的。

我认为我的应用程序第一个版本的流聊天应该是:主要 Activity onCreate(创建 LinearLayout 及其 surfaceView、相机按钮的位置),surfaceCreated 和 surfaceChanged 被调用,onResume.

所以我可以简单地将 surfaceCreated 和 surfaceChanged 中的命令移动到 onResume。但它不起作用!我使用 Debug 发现表面为空。我认为 surfaceCreatd 没有被调用。我的应用程序是不同的 OCRTest,它在主要 Activity 中实现 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

最佳答案

你明白了

java.lang.RuntimeException: Fail to connect to camera service

异常,当其他应用程序正在使用相机时。但我相信其他应用程序就是您的应用程序本身。你必须释放相机。

这些是你应该释放相机的情况:

在预览类中:

public void surfaceDestroyed(SurfaceHolder holder) {

// empty. Take care of releasing the Camera preview in your activity.
if (mCamera != null) {
mCamera.release();
}
}

在 Activity 中:

@Override
public void onBackPressed() {
super.onBackPressed();
if (myCamera != null) {
myCamera.release();
}
finish();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (myCamera != null) {
myCamera.release();
}
}

还有点击“取消”,以及当您完成使用相机时,即当图像被捕获并且您返回 Activity 时。

也试试这个:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

if (mHolder.getSurface() == null) {
return;
}

try {
mCamera.stopPreview();
} catch (Exception e) {
//You can ignore this, because this means the Preview doesn't Exist
//So, no need to try stopping
}

try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
//Catch this
}
}

关于Android 相机 API 应用程序在 onResume 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14191061/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com