Method called after exception throw () exception, unable to resume work with android camera

When developing an application for the camera, I came across an exception that occurred only when switching to another application ( onPause() for my application).

 01-15 17:22:15.017: E/AndroidRuntime(14336): FATAL EXCEPTION: main 01-15 17:22:15.017: E/AndroidRuntime(14336): java.lang.RuntimeException: Method called after release() 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.hardware.Camera.setPreviewDisplay(Native Method) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.hardware.Camera.setPreviewDisplay(Camera.java:357) 01-15 17:22:15.017: E/AndroidRuntime(14336): at com.sora.cbir.yuki.image.leaf.CameraPreview.surfaceCreated(CameraPreview.java:32) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.SurfaceView.updateWindow(SurfaceView.java:551) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:213) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.View.dispatchWindowVisibilityChanged(View.java:4075) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:742) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:742) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:742) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:742) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewRoot.performTraversals(ViewRoot.java:858) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.view.ViewRoot.handleMessage(ViewRoot.java:1995) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.os.Handler.dispatchMessage(Handler.java:99) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.os.Looper.loop(Looper.java:150) 01-15 17:22:15.017: E/AndroidRuntime(14336): at android.app.ActivityThread.main(ActivityThread.java:4389) 01-15 17:22:15.017: E/AndroidRuntime(14336): at java.lang.reflect.Method.invokeNative(Native Method) 01-15 17:22:15.017: E/AndroidRuntime(14336): at java.lang.reflect.Method.invoke(Method.java:507) 01-15 17:22:15.017: E/AndroidRuntime(14336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 01-15 17:22:15.017: E/AndroidRuntime(14336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 01-15 17:22:15.017: E/AndroidRuntime(14336): at dalvik.system.NativeStart.main(Native Method) 

I did some research and found out what I need to add

 mCamera.setPreviewCallback(null); 

as a workaround for android camera stack

my onPause() now looks like this:

 @Override protected void onPause() { super.onPause(); try { // release the camera immediately on pause event //releaseCamera(); mCamera.stopPreview(); mCamera.setPreviewCallback(null); mCamera.release(); mCamera = null; } catch(Exception e) { e.printStackTrace(); } } 

and my onResume() :

 @Override protected void onResume() { super.onResume(); try { mCamera.setPreviewCallback(null); mCamera = getCameraInstance(); //mCamera.setPreviewCallback(null); mPreview = new CameraPreview(Imageupload.this, mCamera);//set preview preview.addView(mPreview); } catch (Exception e){ Log.d(TAG, "Error starting camera preview: " + e.getMessage()); } } } 

and finally my getCameraInstance() method:

 public Camera getCameraInstance(){ Camera camera = null; try { camera = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } Camera.Parameters parameters = camera.getParameters(); //mPreviewSize = getBestPreviewSize(parameters, wt, ht); //mPictureSize = getBestPictureSize(parameters, wt, ht); //Shift W & H => if camera rotates 90 deg mPreviewSize = getOptimalPreviewSize(parameters, wt, ht); //original => wt,ht mPictureSize = getOptimalPictureSize(parameters, wt, ht); //original => wt,ht Log.d("CAMERA", "SCREEN RESOLUTION H: "+ht); Log.d("CAMERA", "SCREEN RESOLUTION W: "+wt); Log.d("CAMERA", "PREVIEW RESOLUTION H: "+mPreviewSize.height); Log.d("CAMERA", "PREVIEW RESOLUTION W: "+mPreviewSize.width); Log.d("CAMERA", "PICTURE RESOLUTION H: "+mPictureSize.height); Log.d("CAMERA", "PICTURE RESOLUTION W: "+mPictureSize.width); //set preview size based on device screen parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height); //set picture size based on device screen parameters.setPictureSize(mPictureSize.width, mPictureSize.height); //set output camera mode parameters.setPictureFormat(PixelFormat.JPEG); //set focous mode parameters.setFocusMode(FOCUS_MODE_AUTO); //set flash mode parameters.setFlashMode("auto"); List<int[]> fps = parameters.getSupportedPreviewFpsRange(); //System.out.println("FPS size: " +fps.size()); //System.out.println("MAX FPS:"+(fps.get(fps.size()-1)[1])/1000); //log min and max camera supported fps Log.d("CAMERA", "CAMERA MAX FPS: "+(fps.get(fps.size()-1)[1])/1000); Log.d("CAMERA", "CAMERA MIN FPS: "+(fps.get(fps.size()-1)[0])/1000); if(camera_fps) { parameters.setPreviewFpsRange(fps.get(fps.size()-1)[1], fps.get(fps.size()-1)[1]); } //set camera parameters camera.setParameters(parameters); Toast.makeText(getApplicationContext(), "Your device are capable of previewing @" + fps.get(fps.size()-1)[1]/1000+"fps!",Toast.LENGTH_SHORT).show(); return camera; // returns null if camera is unavailable } 

any ideas on how to fix this?

+55
java android callback exception camera
Jan 15 '12 at 9:29
source share
9 answers

I have the same problem. mCamera.setPreviewCallback(null); did not help. In my activity, I added this to releaseCamera :

 mPreview.getHolder().removeCallback(mPreview); 

and now it works.

+136
Nov 13
source share
Decision

@ ookami.kb also worked for me, as well as @srunni commented.

 public void onPause() { super.onPause(); if (mCamera != null) { mCamera.setPreviewCallback(null); mPreview.getHolder().removeCallback(mPreview); mCamera.release(); } } 

I also deleted the ondestroy method.

+20
Mar 12 '13 at 10:09
source share

The docs clearly state that camera.release() frees up all camera resources. After that, the call camera link can no longer be used.

If you want to use the camera again, you need to purchase it using the open(int) method.

All of this is described in the camera docs .

+10
Jan 15 2018-12-12T00:
source share

To resume correctly, you need to do the following:

 @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 you to do nothing in oncreate (), except for defining the preview of FrameLayout and Button captureButton.

+5
Aug 07 '14 at
source share
 @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { this.getHolder().removeCallback(this); mCamera.stopPreview(); mCamera.release(); mCamera = null; Log.e("surfaceDestroyed", "surfaceDestroyed"); } 

And re-initialize the camera in the resume function.

+2
Dec 28 '12 at 9:40
source share

Adding okambi to the answer.

This function runs everything when you resume:

  public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, now tell the camera where to draw the preview. try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (IOException e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } 

When you try {}, an exception is not thrown. Namely, that mCamera does not exist, and then when it tries to call setPreviewDisplay (holder), a crash occurs.

Thus, by removing the callback, this machined surface is not called and avoids a failure.

This is VERY MULTIPLE Google DOCUMENTS.

+1
Nov 24 '14 at 18:21
source share

I ran into the same problem, I fixed it - Adding mCamera = null; to the surfaceDestroyed (SurfaceHolder) method of the Preview class.

 public void surfaceDestroyed(SurfaceHolder holder) { // Surface will be destroyed when we return, so stop the preview. if (mCamera != null) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } } 

and - Addition

 camera = Camera.open(); camera.startPreview(); params = camera.getParameters(); preview.setCamera(camera); 

in the OnResume () method of my CameraActivity.

0
Dec 03 '15 at 11:32
source share

If you have:

Attempting to call the virtual method 'void android.hardware.Camera.setPreviewCallback (android.hardware.Camera $ PreviewCallback) "using a null object reference

I agree with @ ookami.kb - mCamera.setPreviewCallback(null); not enough, follow it also add this:

 mCameraView.getHolder().removeCallback(mCameraView); 
0
May 11 '17 at 7:17
source share

I put

 mPreview.getHolder().removeCallback(mPreview); 

between them.

 mCamera.setPreviewCallback(null); 

and

 mCamera.release(); 

and it worked for me.

  @Override protected void onPause() { super.onPause(); this.saveTextEdits(); try { mCamera.stopPreview(); mCamera.setPreviewCallback(null); **mPreview.getHolder().removeCallback(mPreview);** mCamera.release(); mCamera = null; }catch (Exception e){ } } 
0
May 29 '17 at 10:51
source share



All Articles