The Camera
- Introduction
- The camera app that comes with an Android phone can be used to take pictures and record
video.
- For an emulator, make sure you have at least a backfacing camera.
- Set the AVD camera scene to "Emulated" (well, I had to in order to get things working).
- Using the camera application on the phone
- Declare that your app uses the camera, to provent it being installed on a device that
doesn't have a camera
<uses-feature android:name="android.hardware.camera" />
- Put permission to use the camera in the manifest
<uses-permission android:name="android.permission.CAMERA" />
- To use the sdcard you need permission in the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and also from the user, using an ActivityResultLauncher with
ActivityResultContracts.RequestMultiplePermissions.
- The camera app needs more permission to write to the sdcard via a URI provider in
the manifest.
- Taking a photo
- Use an ActivityResultLauncher with
ActivityResultContracts.TakePicture.
- Pass in a Uri where the photo should be stored, get back a
Boolean indicating success.
- Use ActivityResultContracts.TakePicturePreview for a small preview picture.
- Recording a video
- Use an ActivityResultLauncher with
ActivityResultContracts.TakeVideo.
(ActivityResultContracts.CaptureVideo apparently fixes
some bugs, but I could not get it to compile.)
- Pass in a Uri where the video should be stored, get back a
Bitmap indicating nothing.
- Example
MainActivity.java
activity_main.xml
styles.xml
strings.xml
dimens.xml
AndroidManifest.xml
provider_paths.xml
- Using the Camera2 class
- This enables you to write your own camera app.
- I worked on this for 6 hours, and it's complicated.
- The best information I could get was
from New Zealand
(Watch all three epsodes.)