Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: camera view - app crashes when I don't use a deprecated method

In my app, I am trying to set up the camera. My class extends SurfaceView and implements the SurfaceHolder.Callback methods.

Here is some of my class:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
    private SurfaceHolder mHolder;
    private Camera.Parameters cameraParameters;
    private Camera camera;

    public CameraPreview(Context context) {
        super(context);
        mHolder = this.getHolder();
        mHolder.addCallback(this);
        // If this is deprecated, why do I still need it?
        // It says deprecated, but app crashes when removed.
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

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

        Camera.Size previewSize = previewSizes.get(0);
        parameters.setPreviewSize(previewSize.width, previewSize.height);
        camera.setParameters(parameters);

        try {
            camera.stopPreview();
            camera.setPreviewDisplay(mHolder);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
        cameraParameters = camera.getParameters();
}

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }
}

My question involves the "setType" method in the constructor. The API claims the method is deprecated and ignored. However, if I comment out that one line, the entire application crashes when I call camera.startPreview(). I am trying to figure out why this is. If it's ignored, then it shouldn't matter what I did with that method. It implies that there is something very wrong with this implementation.

I am running Android 2.2 software.

Any help would be appreciated.

like image 285
DeeV Avatar asked Jun 10 '11 18:06

DeeV


1 Answers

With Android 2.2 sdk setType method is not deprecated as I have already used that.

So might be there can be changes with other scenarios.

(1) Check the package you have imported is android.hardware.Camera. (2) Check all permissions added in the manifest file for camera.

like image 96
Pratik Sharma Avatar answered Oct 15 '22 15:10

Pratik Sharma