method called after release() exception unable to resume with android camera

后端 未结 9 660
無奈伤痛
無奈伤痛 2020-11-29 00:14

While developing a camera app I\'ve encountered an exception that only happened when I switch to other app (onPause() for my app).

01-15 17:22:1         


        
9条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 00:38

    To Resume correctly, you need to do this:

    @Override
    public void onResume() {
        super.onResume();  
    
        // Get the Camera instance as the activity achieves full user focus
        if (mCamera == null) {
            initializeCamera(); // Local method to handle camera initialization
        }
    }
    
    
    
    protected void initializeCamera(){
        // Get an instance of Camera Object
        mCamera = getCameraInstance();
    
       // create a basic camera preview class that can be included in a View layout.
        mPreview=new CameraPreview(this,mCamera);
    
        //add your preview class to the FrameLayout element.
        preview.addView(mPreview);
    
       //Trigger capturing an image by calling the Camera.takePicture() method.
        captureButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // get an image from the camera
                        mCamera.takePicture(null, null, mPicture);
                    }
                }
            );
    }
    

    And also just to remind that in oncreate() do nothing except defining FrameLayout preview and Button captureButton.

提交回复
热议问题