From 3f97bc25d4e98ef8fbf54d5cdbaecb434db02780 Mon Sep 17 00:00:00 2001
From: Mark Goldstein Note: If you are using the camera via an
-intent, your application does not need to request this permission. Note: If you are using the camera by
+invoking an existing camera app,
+your application does not need to request this permission. A quick way to enable taking pictures or videos in your application without a lot of extra code
-is to use an {@link android.content.Intent} to invoke an existing Android camera application. A
-camera intent makes a request to capture a picture or video clip through an existing camera app and
-then returns control back to your application. This section shows you how to capture an image or
-video using this technique. The procedure for invoking a camera intent follows these general steps: Capturing images using a camera intent is quick way to enable your application to take pictures
-with minimal coding. An image capture intent can include the following extra information: The following example demonstrates how to construct a image capture intent and execute it.
-The {@code getOutputMediaFileUri()} method in this example refers to the sample code shown in Saving Media Files. When the {@link android.app.Activity#startActivityForResult(android.content.Intent, int)
-startActivityForResult()} method is executed, users see a camera application interface.
-After the user finishes taking a picture (or cancels the operation), the user interface returns to
-your application, and you must intercept the {@link
-android.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()}
-method to receive the result of the intent and continue your application execution. For information
-on how to receive the completed intent, see Receiving camera intent
-result. Capturing video using a camera intent is a quick way to enable your application to take videos
-with minimal coding. A video capture intent can include the following extra information: The following example demonstrates how to construct a video capture intent and execute it.
-The {@code getOutputMediaFileUri()} method in this example refers to the sample code shown in Saving Media Files. When the {@link
-android.app.Activity#startActivityForResult(android.content.Intent, int)
-startActivityForResult()} method is executed, users see a modified camera application interface.
-After the user finishes taking a video (or cancels the operation), the user interface
-returns to your application, and you must intercept the {@link
-android.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()}
-method to receive the result of the intent and continue your application execution. For information
-on how to receive the completed intent, see the next section. Once you have constructed and executed an image or video camera intent, your application must be
-configured to receive the result of the intent. This section shows you how to intercept the callback
-from a camera intent so your application can do further processing of the captured image or
-video. In order to receive the result of an intent, you must override the {@link
-android.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} in the
-activity that started the intent. The following example demonstrates how to override {@link
-android.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} to
-capture the result of the image camera intent or video camera intent examples shown in the previous sections. Once your activity receives a successful result, the captured image or video is available in the
-specified location for your application to access.
<uses-permission android:name="android.permission.CAMERA" />
- Using Existing Camera Apps
+Using Existing Camera Apps
-
-
-
-
-
- Image capture intent
-
-
-
-
-private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
-private Uri fileUri;
-
-@Override
-public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- // create Intent to take a picture and return control to the calling application
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
-
- fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
- intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
-
- // start the image capture Intent
- startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
-}
-
-
-Video capture intent
-
-
-
-
-private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
-private Uri fileUri;
-
-@Override
-public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- //create new Intent
- Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
-
- fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video
- intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
-
- intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
-
- // start the Video Capture Intent
- startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
-}
-
-
-Receiving camera intent result
-
-private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
-private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
-
-@Override
-protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
- if (resultCode == RESULT_OK) {
- // Image captured and saved to fileUri specified in the Intent
- Toast.makeText(this, "Image saved to:\n" +
- data.getData(), Toast.LENGTH_LONG).show();
- } else if (resultCode == RESULT_CANCELED) {
- // User cancelled the image capture
- } else {
- // Image capture failed, advise user
- }
- }
-
- if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
- if (resultCode == RESULT_OK) {
- // Video captured and saved to fileUri specified in the Intent
- Toast.makeText(this, "Video saved to:\n" +
- data.getData(), Toast.LENGTH_LONG).show();
- } else if (resultCode == RESULT_CANCELED) {
- // User cancelled the video capture
- } else {
- // Video capture failed, advise user
- }
- }
-}
-
-
-
Some developers may require a camera user interface that is customized to the look of their -application or provides special features. Creating a customized camera activity requires more -code than using an intent, but it can provide a more compelling experience -for your users.
+application or provides special features. Writing your own picture-taking code +can provide a more compelling experience for your users. Note: The following guide is for the older, deprecated {@link android.hardware.Camera}
API. For new or advanced camera applications, the newer {@link android.hardware.camera2} API is
@@ -419,7 +239,7 @@ android.hardware.Camera#getNumberOfCameras() Camera.getNumberOfCameras()} method
If you have determined that the device on which your application is running has a camera, you
must request to access it by getting an instance of {@link android.hardware.Camera} (unless you
-are using an intent to access the camera). Accessing cameras
To access the primary camera, use the {@link android.hardware.Camera#open() Camera.open()} method and be sure to catch any exceptions, as shown in the code below: