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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With