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

Gee picture Gee · Dec 3, 2014 · Viewed 85k times · Source

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

Answer

Eddy Talvala picture Eddy Talvala · Feb 9, 2015

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