Android: camera view - application crashes when I don't use the deprecated method

In my application, I am trying to configure the camera. My class extends SurfaceView and implements SurfaceHolder.Callback methods.

Here are some of my classes:

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 includes the setType method in the constructor. The API claims that the method is deprecated and ignored. However, if I comment that one line, the whole application will work when I call camera.startPreview (). I am trying to understand why this is so. If it is ignored, then it doesn't matter what I did with this method. This means that there is something very bad in this implementation.

I am running Android 2.2 software.

Any help would be appreciated.

+6
source share
1 answer

With Android 2.2 sdk, the setType method is not deprecated since I already used this.

Therefore, there may be changes with other scenarios.

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

+1
source

Source: https://habr.com/ru/post/890247/


All Articles