0

I'm facing following problem. I'm using SurfaceView to show Camera preview, but when I set surface view to visible, I get following exception.

java.lang.RuntimeException: getParameters failed (empty parameters)
at android.hardware.Camera.native_getParameters(Native Method)
at android.hardware.Camera.getParameters(Camera.java:1460)
at com.the.package.activity.ShowPhotoActivity$2.surfaceChanged(ShowPhotoActivity.java:111)
at android.view.SurfaceView.updateWindow(SurfaceView.java:558)
at android.view.SurfaceView.setVisibility(SurfaceView.java:248)

ShowPhotoActivity:

@Override                                                                                    
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {        
    Camera.Parameters parameters = mCamera.getParameters(); // <-- line 101                                 
    Camera.Size size = getBestPreviewSize(width, height, parameters);                        

    if (mInPreview) {                                                                        
        mCamera.stopPreview();                                                               
    }                                                                                        

    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();


    if (display.getRotation() == Surface.ROTATION_0) {                                       
        mCamera.setDisplayOrientation(90);                                                   
        parameters = mCamera.getParameters(); // <---- line 111                                            
        size = getBestPreviewSize(width, height, parameters);                                
    }                                                                                        

    if (display.getRotation() == Surface.ROTATION_270) {                                     
        mCamera.setDisplayOrientation(180);                                                  
        parameters = mCamera.getParameters();                                                
        size = getBestPreviewSize(width, height, parameters);                                
    }                                                                                                                                                

    Camera.Size s = getBestResolution(parameters);                                           
    parameters.setPreviewSize(size.width, size.height);                                      
    parameters.setPictureSize(s.width, s.height);                                            

    mCamera.setParameters(parameters);                                                       
    if (mInPreview) {                                                                        
        mCamera.startPreview();                                                              
    }                                                                                        
}                     

It really drives me crazy, why line 101 is processed with no problem and line 111 throws exception. This happens only on device Sony st26i (Xperia J) with Android 4.1.1. I haven't noticed any problems on Galaxy Nexus, Galaxy SIII or 4.1.2 emulator. Thanks for every idea.

skywall
  • 3,956
  • 1
  • 34
  • 52
  • take a look at this: http://stackoverflow.com/questions/14941625/correct-handling-of-exception-getparameters-failed-empty-parameters – mthandr Nov 26 '14 at 16:39
  • Thanks, I have already tried this, but doesn't work. – skywall Nov 26 '14 at 16:50
  • Do you have access to one of the failing devices? Or at least some way to get at the logcat output when the failure occurs? – fadden Nov 26 '14 at 16:54
  • I have only logcat I've posted. I've just removed some of my function calls. – skywall Nov 26 '14 at 16:59

1 Answers1

1

It has been answered over here, check the accepted answer: Correct handling of exception: "getParameters failed (empty parameters)"

My guess is that stopPreview here is problematic, potentially called from the wrong thread. Check your logs around the time you call stopPreview, it might give you a clue.

Community
  • 1
  • 1
Sascha
  • 86
  • 5
  • It's OK to call stopPreview() this way, but the camera service may perform this request asynchronously. Be prepared to catch RuntimeException doing most innocent camera calls. But in this specific case, there is no justification to call getParameters() again. setDisplayOrientation() will not affect the supported preview sizes. – Alex Cohn Nov 26 '14 at 17:49