0

I'm trying to create an Android app which supports both Camera and Camera2, and I have done that by using if statements which check the API level for appropriate sections of code. However, I'm having trouble coping with the CameraAccessException in that I'm getting a lint error, but I'm not sure how to get rid of it. If I surround the try/catch with an if statement which checks the API level, the lint error doesn't go away. Is there any other way of getting rid of the lint error? I'd rather not suppress inspections. Here is an example of what I'm talking about:

private String getFrontCamera2() {
    if (Build.VERSION.SDK_INT >= 21) {
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        String[] cameraIdList;
        try {
            cameraIdList = cameraManager.getCameraIdList();
        } catch (CameraAccessException e) {
            return null;
        }
        for (String cameraId : cameraIdList) {
            CameraCharacteristics cameraCharacteristics = null;
            try {
                cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
            } catch (CameraAccessException e) {
                return null;
            }
            Integer lensFacing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
            if (lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_FRONT) {
                return cameraId;
            }
        }
    }
    return null;
}

Both catch statements in that piece of code are generating the lint error "Class requires API level 21".

Chris B
  • 401
  • 6
  • 10

1 Answers1

0

I just learned about the @TargetApi annotation, which solves my problem.

Chris B
  • 401
  • 6
  • 10