realuser
realuser

Reputation: 951

Camera app crashed some devices?

I have an application that taking a photo in android. It crashed some devices. I set the photo size as follows. What could be the reason for the crash?

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
    Camera.Parameters parameters = camera.getParameters();
    List<Size> sizes = parameters.getSupportedPictureSizes();
    if (sizes == null || sizes.size() == 0)
    {
        parameters.setPreviewSize(w, h);
    } else
    {
        parameters.setPictureSize(sizes.get(0).width, sizes.get(0).height);
        for(Size s : sizes)
        {
            if( s.width < 700)
            {
                parameters.setPictureSize(s.width, s.height);
                break;
            }
        }
    }

    camera.setParameters(parameters);
    camera.startPreview();
}

Upvotes: 0

Views: 825

Answers (2)

Klaus Villaca
Klaus Villaca

Reputation: 587

Guys I know that it's a old post though I came across the same issue though this time everything worked on Samsung and Nexus line though not on HTC (due Sense for sure). What I've done to fix this issue was loop through all supported sizes and get the one that is supported. In fact it's crash when I did try to setParamters, because the maximun Picture size supported value. So I did a small method that loop through it and set the one that is accepted by the device.

private void setCameraParameters() {
    if (camera != null) {
        mParameters = camera.getParameters();
        List<Camera.Size> sizes = mParameters.getSupportedPreviewSizes();
        Camera.Size selected = sizes.get(0);
        if (android.os.Build.MANUFACTURER.contains("HTC")) {
            getHTCBestSupportedResolution(sizes);
        }
        mParameters.setColorEffect(Camera.Parameters.EFFECT_MONO);
        mParameters.setJpegQuality(100);
        mParameters.setPreviewSize(selected.width, selected.height);
        mParameters.setRotation(90);
        mParameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
        mParameters.setPictureFormat(ImageFormat.JPEG);
        camera.setParameters(mParameters);
        camera.setDisplayOrientation(90);
        camera.startPreview();
    }
}


private void getHTCBestSupportedResolution(List<Camera.Size> sizes) {
    for (Size size : sizes) {
        try {
            mParameters.setPictureSize(size.width, size.height);
            camera.setParameters(mParameters);
            // Log.e("Size worked", size.width + " x " + size.height);
            break;
        } catch (Exception e) {
            continue;
        }
    }
}

Now it's works fine on all devices, as rule of thumb you can remove the condition that compare for HTC and just let it generic for all devices. I hope that this help those ones facing the same issue. Thanks

Upvotes: 1

Lennart
Lennart

Reputation: 10354

Check Logcat, I guess it crashes on

camera.setParameters(parameters);

On some devices some of the reported supported resolutions don't seem to be actually supported. You can try to find a legacy resolution, that is supported on all devices. Or catch the exception and try to set another resolution.

Upvotes: 0

Related Questions