Taking a picture using the Camera2 (the new) API

Now that we've looked at the old Camera API, it's time to learn about the new Camera2 API. Unfortunately, it's a bit more complicated due to the asynchronous nature of the APIs. Fortunately, the overall concept is the same as the previous Camera API.

Getting ready

Create a new project in Android Studio and call it Camera2API. On the Target Android Devices dialog, select the Phone & Tablet option and chose API 21 (or higher) for the Minimum SDK. Select Empty Activity when prompted for Activity Type.

How to do it...

As you'll see, there's a lot of code for this recipe. Start by opening the Android Manifest and following these steps:

  1. Add the following two permissions:
    <uses-permission android:name="android.permission.CAMERA" ...

Get Android Application Development Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.