所以,我正在制作相机应用程序,我不了解所有代码,但大多数代码。我从网上复制了一些代码。我的活动一直强制关闭表面破坏方法。当我点击图片时,我无法分辨为什么它会一直关闭。因为当我点击图片时,我会启动一个Intent来进行新的活动。那是表面被破坏的时候。顺便说一下,我的表面是我相机预览正在进行的相对布局。错误发生在OnSurfaceDestroyed方法的ShowCamera类中。我可以看到预览。当我尝试点击图片时应用关闭。
这是我的代码。从我的MainActivity.java中,我在onCreateMethod
中发送以下代码Camera frontCam = Camera.open(1) // 1 is for front cam
Show Camera showCamera = new ShowCamera(this, frontCam);
从MainActivity.java我发送下面的代码onCapture of a picture
private PictureCallback capturedIt = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if (bitmap == null) {
Toast.makeText(getApplicationContext(), "not taken",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "taken",
Toast.LENGTH_SHORT).show();
Intent viewerIntent = new Intent("com.tyrone.mirrorapp.AFTERCAPTURE");
viewerIntent.setClass(MainActivity.this, afterCapture.class);
Bundle bitmapBundle = new Bundle();
bitmapBundle.putByteArray("clickedBitmap", data);
viewerIntent.putExtras(bitmapBundle);
startActivityForResult(viewerIntent,0);
}
cameraObject.release();
}
};
这是我下面的ShowCamera.java类
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holdMe;
Camera theCamera;
public ShowCamera(Context context,Camera camera) {
super(context);
theCamera = camera;
holdMe = getHolder();
holdMe.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int width, int height) {
theCamera.setDisplayOrientation(90);//cz the fron cam doesn't adjust.
theCamera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
theCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
theCamera.setPreviewCallback(null);
theCamera.stopPreview();
theCamera.release();
theCamera = null;
}
}
我的活动一直强制关闭活动的破坏。任何想法可能是什么原因? 附: :我不完全确定它强制关闭SurfaceDestroyed。如果我错了,请纠正我。谢谢
我的日志文件的一部分
11-18 20:30:31.877: I/SurfaceView(21648): Callback --> surfaceCreated
11-18 20:30:31.877: I/SurfaceView(21648): surfaceCreated callback +
11-18 20:30:31.877: D/setting preview display(21648): getting Ready
11-18 20:30:31.879: D/AndroidRuntime(21648): Shutting down VM
11-18 20:30:31.879: W/dalvikvm(21648): threadid=1: thread exiting with uncaught exception (group=0x41a00c98)
11-18 20:30:31.879: W/dalvikvm(21648): threadid=1: uncaught exception occurred
11-18 20:30:31.879: W/System.err(21648): java.lang.RuntimeException: Method called after release()
11-18 20:30:31.880: W/System.err(21648): at android.hardware.Camera.setPreviewDisplay(Native Method)
11-18 20:30:31.880: W/System.err(21648): at android.hardware.Camera.setPreviewDisplay(Camera.java:580)
11-18 20:30:31.880: W/System.err(21648): at com.tyrone.mirrorapp.ShowCamera.surfaceCreated(ShowCamera.java:66)
11-18 20:30:31.880: W/System.err(21648): at android.view.SurfaceView.updateWindow(SurfaceView.java:662)
11-18 20:30:31.881: W/System.err(21648): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:256)
11-18 20:30:31.881: W/System.err(21648): at com.tyrone.mirrorapp.ShowCamera.onWindowVisibilityChanged(ShowCamera.java:98)
11-18 20:30:31.881: W/System.err(21648): at android.view.View.dispatchWindowVisibilityChanged(View.java:8096)
11-18 20:30:31.881: W/System.err(21648): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
11-18 20:30:31.881: W/System.err(21648): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
11-18 20:30:31.881: W/System.err(21648): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
11-18 20:30:31.881: W/System.err(21648): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
11-18 20:30:31.881: W/System.err(21648): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
11-18 20:30:31.881: W/System.err(21648): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1448)
11-18 20:30:31.881: W/System.err(21648): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
11-18 20:30:31.881: W/System.err(21648): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
11-18 20:30:31.881: W/System.err(21648): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
11-18 20:30:31.882: W/System.err(21648): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
11-18 20:30:31.882: W/System.err(21648): at android.view.Choreographer.doFrame(Choreographer.java:560)
11-18 20:30:31.882: W/System.err(21648): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
11-18 20:30:31.882: W/System.err(21648): at android.os.Handler.handleCallback(Handler.java:808)
11-18 20:30:31.882: W/System.err(21648): at android.os.Handler.dispatchMessage(Handler.java:103)
11-18 20:30:31.882: W/System.err(21648): at android.os.Looper.loop(Looper.java:193)
11-18 20:30:31.882: W/System.err(21648): at android.app.ActivityThread.main(ActivityThread.java:5292)
11-18 20:30:31.882: W/System.err(21648): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 20:30:31.882: W/System.err(21648): at java.lang.reflect.Method.invoke(Method.java:515)
11-18 20:30:31.883: W/System.err(21648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
11-18 20:30:31.883: W/System.err(21648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
11-18 20:30:31.883: W/System.err(21648): at dalvik.system.NativeStart.main(Native Method)
11-18 20:30:31.883: W/dalvikvm(21648): threadid=1: calling UncaughtExceptionHandler
11-18 20:30:31.885: E/AndroidRuntime(21648): FATAL EXCEPTION: main
11-18 20:30:31.885: E/AndroidRuntime(21648): Process: com.tyrone.mirrorapp, PID: 21648
11-18 20:30:31.885: E/AndroidRuntime(21648): java.lang.RuntimeException: Method called after release()
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.hardware.Camera.setPreviewDisplay(Native Method)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.hardware.Camera.setPreviewDisplay(Camera.java:580)
11-18 20:30:31.885: E/AndroidRuntime(21648): at com.tyrone.mirrorapp.ShowCamera.surfaceCreated(ShowCamera.java:66)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.SurfaceView.updateWindow(SurfaceView.java:662)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:256)
11-18 20:30:31.885: E/AndroidRuntime(21648): at com.tyrone.mirrorapp.ShowCamera.onWindowVisibilityChanged(ShowCamera.java:98)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.View.dispatchWindowVisibilityChanged(View.java:8096)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1110)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1448)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.Choreographer.doFrame(Choreographer.java:560)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.os.Handler.handleCallback(Handler.java:808)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.os.Handler.dispatchMessage(Handler.java:103)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.os.Looper.loop(Looper.java:193)
11-18 20:30:31.885: E/AndroidRuntime(21648): at android.app.ActivityThread.main(ActivityThread.java:5292)
11-18 20:30:31.885: E/AndroidRuntime(21648): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 20:30:31.885: E/AndroidRuntime(21648): at java.lang.reflect.Method.invoke(Method.java:515)
11-18 20:30:31.885: E/AndroidRuntime(21648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
11-18 20:30:31.885: E/AndroidRuntime(21648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
11-18 20:30:31.885: E/AndroidRuntime(21648): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您的日志指出异常发生在SurfaceCreated上。在调用此回调之前释放 theCamera 对象。请检查您在应用中释放相机的位置。也许,您在{strong 1}}中将 frontCam 传递给 ShowCamera 构造函数后将其释放。
好的,正如您所解释的那样,当您从另一个返回MainActivity时会发生这种情况。通常的做法是在onCreate()
)中打开相机,然后在onResume(
中发布。也可以在onPause()
中打开相机,然后在ShowCamera.surfaceCreated()
中发布(如上面的代码所示)。
建议在后台线程中打开相机,但这需要更精细地调用ShowCamera.surfaceDestroyed()
。