How to use Android's camera or camera2 API to support old and new API versions without deprecation notes?

Even though the old camera API is marked as deprecated, it is still fully functional, and will remain so for quite a while (as nearly all camera-using applications on the Play Store use it currently).

You'll have to ignore Android Studio's complaints about it being deprecated, but if you want to support Android versions earlier than 21, you have to use the old API.

On API level 21, you can certainly use the new API and its new features, but currently you'll have to maintain a wholly separate flow in your app if you switch between the APIs. Unfortunately, the two APIs have a different enough of a worldview that it's hard to write a support library that would let you use something like the new API on older devices as well (where the library maps from the new API to the old API if not on API 21+).


Put all the methods from the camera that you need in an interface and then create a camera instance like this

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Log.d(TAG, "camera2 selected");
        this.camera = new Camera2(getContext());
    } else {
        Log.d(TAG, "camera1 selected");
        this.camera = new Camera1(getContext());
    }

This way you will have everything split up and it will make your life so much easier.

Word of advice - life with camera2 isn't that great. Venders still make crap implementations and you will thus have to add lots of conditions and workarounds.

Example 1 - S6 reports that it doesn't support flash :) Example 2 - An LG device reports back a list of supported image sizes - however not all of them are actually supported!!


To support api you want, use the code below. Just determine the appropriate names corresponded api levels. For example, API 21 is LOLLIPOP, and API 15 is ICE_CREAM_SANDWICH_MR1.

 if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)  
                                    && ((Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP))) {
           // your code here - is between 15-21

 } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
           // your code here - is api 21
 }