59

I have included the camera functionality in my application. I have also launched the app in the market. I got an error message from one of the users that he is getting an error while opening the camera.

I have tested the app on the device on 2.1. The error I got from the user is using nexus one which will mostly run 2.2...Here's the logcat error that I've received...

java.lang.RuntimeException: setParameters failed
at android.hardware.Camera.native_setParameters(Native Method)
at android.hardware.Camera.setParameters(Camera.java:647)
at com.CameraApp.Preview.surfaceChanged(Preview.java:67)
at android.view.SurfaceView.updateWindow(SurfaceView.java:538)
at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339)
at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
at android.view.View.draw(View.java:6743)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
at android.view.View.draw(View.java:6743)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
at android.view.ViewRoot.draw(ViewRoot.java:1407)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

I ran the app on 2.2 emulator also to check it. But it worked fine. Can anyone please tell me why the error is occurring?

Here is the class which I mentioned in the comments:

class Preview extends SurfaceView implements SurfaceHolder.Callback 
{
    private static final String TAG = "Preview";

    SurfaceHolder mHolder;
    public Camera camera;

    Preview(Context context) 
    {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) 
    {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
         camera = Camera.open();
        try {
        camera.setPreviewDisplay(holder);
        } catch (IOException exception) {
        camera.release();
        camera = null;
            // TODO: add more exception handling logic here
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) 
    {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        camera.stopPreview();
        camera.release();
        camera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) 
    {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.

        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(w, h);
        camera.setParameters(parameters);
        camera.startPreview();
    }

    @Override
    public void draw(Canvas canvas) 
    {
        super.draw(canvas);
        Paint p= new Paint(Color.RED);
        Log.d(TAG,"draw");
        canvas.drawText("PREVIEW", canvas.getWidth()/2, canvas.getHeight()/2, p );
    }
}
CRM
  • 4,569
  • 4
  • 27
  • 33
Ramesh Bugatha
  • 1,162
  • 1
  • 11
  • 24
  • Unless you post the code where you are modifying the camera parameters, it will be very difficult to help you. – CommonsWare Oct 08 '10 at 13:20
  • thank u for u response I have a separate class which is named as "Preview" in that class i am overriding some methods one them is " surfaceChanged() " which uses "setParameters()", i am adding code of that class in question body please have a look at it... and help me to sort out this issue. thank u – Ramesh Bugatha Oct 09 '10 at 05:13

1 Answers1

122

It is failing because not all devices support arbitrary preview sizes. Apparently some do but you can't rely on it. In your surfaceChanged method you need to do something like this:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    Camera.Parameters parameters = camera.getParameters();
    List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

    // You need to choose the most appropriate previewSize for your app
    Camera.Size previewSize = // .... select one of previewSizes here

    parameters.setPreviewSize(previewSize.width, previewSize.height);
    camera.setParameters(parameters);
    camera.startPreview();
}

You'll have to figure out a way to scale this so that you don't lose the aspect ratio etc.

For reference here is the Android SDK doc.

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
macbutch
  • 3,241
  • 2
  • 27
  • 27
  • thank u i tried the above code, but it is from 2.1 onwards. it is also giving some errors. the is problem when i am changing the orientation of mobile from portrait to landscape it is giving null values for preview in actual device image is stored with 0kb it is working fine in portrait and it is working well in the emulator in both portrait and landscape modes,, – Ramesh Bugatha Oct 19 '10 at 05:05
  • Ok I don't know how to do it before 2.1. I had a similar problem on the HTC Evo and this is how I got around it. The problem is that you can't necessarily set the previewSize to whatever you want (not all phones will support it). The previewSize doesn't necessarily need to be the size of the surface you're drawing into if that helps at all. – macbutch Oct 22 '10 at 22:02
  • Thanks for this one, on 2.2 I had to replace 'getSupportedPreviewSizes' with 'getPreviewSize' but the rest just works. – kolenda Jul 02 '13 at 15:51
  • Hey can you tell me what to write in Camera.Size previewSize = – Pooja Roy May 19 '14 at 06:59
  • 1
    @PoojaRoy Just write `Camera.Size previewSize = previewSizes.get(0);` and see whether it works. The preview sizes are arranged in descending fashion AFAIK. – Ε Г И І И О Aug 29 '14 at 06:00
  • 3
    Its still crash while testing on Huwawei honor 4c – Ahmad Arslan Aug 17 '15 at 08:53
  • 5
    Note that `getSupportedPreviewSizes` is different from `getSupportedPictureSizes`, use first with `setPreviewSize` and second with `setPictureSize`. – iman Oct 10 '16 at 14:13
  • It was not working on my Huawei P10 for the first time, but then I realized that I was setting "parameters.setFocusMode( Parameters.FOCUS_MODE_AUTO );" on my front camera where my front camera does not have auto focus... – Bero Apr 19 '19 at 14:18