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:
- Add the following two permissions:
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- Now open
activity_main.xml
and replace the existing TextView with the following views...