• 如果我不使用 mCamera.release(); 中的 surfaceDestroyed(....),然后无法从另一个 Activity 重新启动CameraActivity [总之,得到不幸的是,应用程序已停止]错误,甚至没有释放Camera ,但如果我在首页上轻按,按钮[CameraActivity 中的],然后再次启动我的应用程序,没有收到任何错误(总之,可以正常工作,并且没有任何问题地打开CameraActivity)
  • 如果我在 surfaceDestroyed(....)中使用 * mCamera.release(); *成为,则能够再次从另一个 Activity 启动CameraActivity,并在ijt_rstrong时释放Camera和,但是在ojit_点击“主页”按钮,然后再次启动我的应用,但不幸的是,该应用已停止 ....:

  • 但是我希望两者一起工作(首先,从CameraActivity轻按Home,然后再次从CameraActivity启动应用
    -无任何错误)和(第二,从另一个 Activity 启动照相机-无任何错误)

    就像我写的一样,这两件事对我都有效,但不能一起使用.....

    行号33是:
            Camera.Parameters parameters = mCamera.getParameters();
    

    完成日志:
    12-30 12:18:58.070: W/dalvikvm(14822): threadid=1: thread exiting with uncaught exception (group=0x41ef72a0)
    12-30 12:18:58.080: E/AndroidRuntime(14822): FATAL EXCEPTION: main
    12-30 12:18:58.080: E/AndroidRuntime(14822): java.lang.RuntimeException: Method called after release()
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.hardware.Camera.native_getParameters(Native Method)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.hardware.Camera.getParameters(Camera.java:1487)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at app.micheal.camr.PreviewSurface.surfaceCreated(PreviewSurface.java:33)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.SurfaceView.updateWindow(SurfaceView.java:609)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:235)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.View.dispatchWindowVisibilityChanged(View.java:7686)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1047)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1339)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1114)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4520)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.Choreographer.doFrame(Choreographer.java:525)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.os.Handler.handleCallback(Handler.java:615)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.os.Handler.dispatchMessage(Handler.java:92)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.os.Looper.loop(Looper.java:137)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at android.app.ActivityThread.main(ActivityThread.java:4921)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at java.lang.reflect.Method.invokeNative(Native Method)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at java.lang.reflect.Method.invoke(Method.java:511)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
    12-30 12:18:58.080: E/AndroidRuntime(14822):    at dalvik.system.NativeStart.main(Native Method)
    12-30 12:19:08.095: I/Process(14822): Sending signal. PID: 14822 SIG: 9
    

    PreviewSurface.java:
    public class PreviewSurface extends SurfaceView implements
    SurfaceHolder.Callback {
    
        public static final String LOG_TAG = "CameraPreview";
        private SurfaceHolder mSurfaceHolder;
    
        private Camera mCamera;
        Camera.Parameters parameters = null ;
    
        // Constructor that obtains context and camera
        @SuppressWarnings("deprecation")
        public PreviewSurface(Context context, Camera camera) {
            super(context);
            this.mCamera = camera;
            this.mSurfaceHolder = this.getHolder();
            this.mSurfaceHolder.addCallback(this);
            this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            this.mSurfaceHolder.setFixedSize(100, 100);
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            try {
                    parameters = mCamera.getParameters();
    
                    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
                    {
                         parameters.set("orientation", "portrait");
                         mCamera.setDisplayOrientation(90);
                         parameters.setRotation(90);
                         mCamera.setPreviewDisplay(surfaceHolder);
                         mCamera.startPreview();
                    }
                    else
                    {
                         // This is an undocumented although widely known feature
                         parameters.set("orientation", "landscape");
                         // For Android 2.2 and above
                         mCamera.setDisplayOrientation(0);
                         // Uncomment for Android 2.0 and above
                         parameters.setRotation(0);
                    }
    
                    mCamera.setPreviewDisplay(surfaceHolder);
                    mCamera.startPreview();
    
                } catch (IOException e) {
                    // left blank for now
                }
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            mCamera.stopPreview();
            mCamera.release();
        }
    
    
        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
                int width, int height) {
    
            try {
                parameters = mCamera.getParameters();
                if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
                     parameters.set("orientation", "portrait");
                     mCamera.setDisplayOrientation(90);
                     parameters.setRotation(90);
    
                }
                     else {
                          // This is an undocumented although widely known feature
                          parameters.set("orientation", "landscape");
                          // For Android 2.2 and above
                          mCamera.setDisplayOrientation(0);
                          // Uncomment for Android 2.0 and above
                          parameters.setRotation(0);
                }
                mCamera.setPreviewDisplay(surfaceHolder);
                mCamera.startPreview();
    
    
            } catch (IOException e) {
                // left blank for now
            }
        }
    
    }
    

    最佳答案

    尝试一下,用我的代码替换您的代码,我想这会为您提供帮助,我还没有尝试过,但是看起来它可以解决您的错误

          @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    
            this.getHolder().removeCallback(this);
            mCamera.stopPreview();
            mCamera.release();
        }
    

    让我尽快知道...

    10-07 20:41