Chris
Chris

Reputation: 5627

Android 2.3.4 Crashes on Camera.setParameters

I have a camera app that works on most phone but I got an error from a user today. The error says that the app crashes on Camera.setParameters() I have read This StackOverflow Post about the topic, but have that same solution already implemented.

Here's the code I'm using:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

        camera.setDisplayOrientation(90);
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = getBestPreviewSize(w, h);    
        parameters.setPreviewSize(size.width, size.height); // preview size
        camera.setParameters(parameters);
        camera.startPreview();


        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


    }


private Camera.Size getBestPreviewSize(int width, int height)
    {

        // Get For Photo Size
        Camera.Parameters camparams = camera.getParameters();

        // Find the Best Preview Size
        List<Size> sizes = camparams.getSupportedPreviewSizes();

        Camera.Size result=null;
        int finalHeight = 0;
        for (Size s : sizes) {

            if (s.width <= width && s.height <= height) {
                if (result == null) {
                        result = s;
                        finalHeight = s.height;
                } else {
                        int resultArea=result.width*result.height; 
                        int newArea=s.width*s.height;

                        if (newArea>resultArea) {
                      result=s;
                      finalHeight = s.height;
                     }
                }
            } 
        }

        // Just in case... 
        if (result == null) {
            finalHeight = height;
        }

        result.width = (int)(finalHeight*cameraRatio);


        return result; 

    }

My thought is to put a try / catch around the camera.setParameter(size.width, size.height) but I don't know if that will keep it from crashing upon failure?

Here is the Crash Log that was sent to me on developer.android.com:

java.lang.RuntimeException: setParameters failed at android.hardware.Camera.native_setParameters(Native Method) at android.hardware.Camera.setParameters(Camera.java:953) at net.feltpad.mosaic.Preview.surfaceChanged(CameraPreview.java:145) at android.view.SurfaceView.updateWindow(SurfaceView.java:557) at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.View.draw(View.java:6988) at android.widget.FrameLayout.draw(FrameLayout.java:357) at android.view.ViewGroup.drawChild(ViewGroup.java:1732) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.View.draw(View.java:6988) at android.widget.FrameLayout.draw(FrameLayout.java:357) at android.view.ViewGroup.drawChild(ViewGroup.java:1732) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.View.draw(View.java:6988) at android.widget.FrameLayout.draw(FrameLayout.java:357) at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1961) at android.view.ViewRoot.draw(ViewRoot.java:1602) at android.view.ViewRoot.performTraversals(ViewRoot.java:1323) at android.view.ViewRoot.handleMessage(ViewRoot.java:1961) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:150) at android.app.ActivityThread.main(ActivityThread.java:4333) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method)

Upvotes: 4

Views: 1828

Answers (2)

Long Thay
Long Thay

Reputation: 13

You can have an extended class for CameraHost. Within this class you can override the adjustPreviewParameters functions which result in the wrong result size. Below is my fix for camera preview:

@Override
public Parameters adjustPreviewParameters(Parameters parameters) {
    List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
    Camera.Size cs = sizes.get(0);
    parameters.setPreviewSize(cs.width, cs.height);
    return super.adjustPreviewParameters(parameters);
}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007359

This code assumes that you can just set result.width to some dynamically-computed value. This will not work on all devices. The result must be one of the values returned by getSupportedPreviewSizes() to work reliably across all devices, and in your case, it is not necessarily one of those values.

Upvotes: 1

Related Questions