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

87,896

Solution 1

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+).

Solution 2

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!!

Solution 3

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
 }

Solution 4

Although, what Google recommend use Camera2 Api >= 21, but you could have problem with manual settings.

When you need implement app for taking photo with Auto Setting Mode, it'll work fine. But! If need create app with Manual Setting Mode implementation, for devices that have API >= 21, firstly, need check supported HARDWARE LEVEL:

Select camera(Front, Face), get it characteristics and check HARDWARE LEVEL.

mCameraCharacteristics = mCameraManager.getCameraCharacteristics(mCameraId)

val level = mCameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL)

CameraCharacteristics represent next supported levels: LIMITED, FULL, LEGACY, LEVEL_3, EXTERNAL.

At a high level, the levels are:

LEGACY devices operate in a backwards-compatibility mode for older Android devices, and have very limited capabilities.

LIMITED devices represent the baseline feature set, and may also include additional capabilities that are subsets of FULL.

FULL devices additionally support per-frame manual control of sensor, flash, lens and post-processing settings, and image capture at a high rate.

LEVEL_3 devices additionally support YUV reprocessing and RAW image capture, along with additional output stream configurations.

If you got the LEGACY supprot level, you should use old Camera Api.

Solution 5

Use the support annotation

    @TargetApi(21)

to avoid checking

Share:
87,896
Gee
Author by

Gee

Updated on January 22, 2022

Comments

  • Gee
    Gee over 2 years

    The new camera2 API confuses me. I want to develop an app (for Android APIs 10 - 21) which uses the device's camera. As stated here, I should use the "Camera" API.

    However, when I try to add the "Camera" API (android.hardware.Camera) to the manifest's user features, it is marked as deprecated. On the other hand, I cannot change it to the "camera2" API (android.hardware.camera2) since it is only compatible with Android API 21+ (Android 5 - Lollipop) - Would have linked it too, but I can only add 2 links.

    Not only do I want my app to run on older versions of Android, but also the newest one...

  • Loolooii
    Loolooii about 9 years
    Good answer. So if you want to support API level 16 and up, it's better to stick to the old camera for now, right?
  • katzenhut
    katzenhut almost 9 years
    this is hardly practical for a full camera implementation. plus, now you have to maintain two codepaths. the version check does have its use in android development, but this isn't it.
  • Ralph Pina
    Ralph Pina almost 9 years
    What happens if a user is running Build.VERSION_CODES.LOLLIPOP_MR1? Or something above that? I think your second check should be "else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)"
  • hadi
    hadi over 8 years
    so the only way is to use if statement and android.os.Build.VERSION.SDK_INT to seperate the code?
  • john.weland
    john.weland over 8 years
    So for a developer, if you're only targeting API 21 and later, use Camera2 but if you need legacy support use Camera? Or would you recommend detecting build versions and coding 2 different methods using the different APIs?
  • Eddy Talvala
    Eddy Talvala over 8 years
    It depends on what your app does. If the camera functionality is straightforward point-and-shoot stuff, and you want to target old APIs, just use the old Camera API. But if you're looking to do something more than just grab JPEGs and draw preview, or if you're just targeting new APIs, go with camera2. In the (hard) middle are apps that want to offer fancy optional features on camera2, but work on old devices as well. There, you have to build two separate codepaths, one for each API.
  • davkutalek
    davkutalek almost 8 years
    My phone running 5.1 (First gen moto x) crashes when attempting to use the old API
  • panonski
    panonski almost 8 years
    This is true. The camera 2 API actually divides camera devices into three categories: LEGACY, LIMITED and FULL. If the camera is classified as LEGACY then all the camera2 API calls are being translated into camera1 under the hood, so it's really not worth the bother. My suggestion is to call CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraID); if (characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HA‌​RDWARE_LEVEL) == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY).‌​.. and choose the old API if it is true.
  • Sudara
    Sudara almost 8 years
    Deprecating the Camera API was a mistake, they should have introduced a Camera advanced API (for advanced apps such as full fledged camera apps) - otherwise (most) apps which uses the camera just to take a photograph would have to maintain 2 apis. Google should have at least introduced a compact library (as always)
  • Mateus
    Mateus over 7 years
    Dears, how can I build in the same apk camera2 and old api if my apps should be work in the 16 and newer api? Flavors is good for this work?
  • user0770
    user0770 over 7 years
    You have to implement both apis. Just keep an interface and two classes, where camera functionality is implemented. Before creating one of the instances for running camera, call the method mentioned above, so it can find out which class and functionality to call
  • Admin
    Admin almost 7 years
    @EddyTalvala Hi Eddy, You say "You'll have to ignore Android Studio's complaints about it being deprecated" but how do you ignore this when it is all underlined red as an error and does not compile?
  • Mina F. Beshay
    Mina F. Beshay over 6 years
    So you don't support the android devices before 21 !
  • MohanRaj S
    MohanRaj S over 6 years
    I agree with @S_Madushan comment, I have to face the issue while recording video and get picture simultaneously, this feature will available on nougat 7+. but how can I get it done? please help.
  • user924
    user924 about 2 years
    how to exclude legacy camera devices from Google Play?
  • user924
    user924 about 2 years
    how to exclude device with LEGACY camera from Google Play? I want to use only Camera2 API and only devices with at least has LIMITED camera