From 3f97bc25d4e98ef8fbf54d5cdbaecb434db02780 Mon Sep 17 00:00:00 2001 From: Mark Goldstein Date: Fri, 26 Aug 2016 11:29:25 -0700 Subject: [PATCH] docs: Fix broken camera example Removed bad out of date instructions and link to better training pages instead. Bug: 15562334 Change-Id: I77fe558e665de509eb11a2c668ba7bdcdbab3c6f --- docs/html/guide/topics/media/camera.jd | 206 ++----------------------- 1 file changed, 13 insertions(+), 193 deletions(-) diff --git a/docs/html/guide/topics/media/camera.jd b/docs/html/guide/topics/media/camera.jd index c806c886a3cc0..4995a13dda91e 100644 --- a/docs/html/guide/topics/media/camera.jd +++ b/docs/html/guide/topics/media/camera.jd @@ -9,12 +9,7 @@ page.tags=photo,video,picture,mediarecorder
  • Considerations
  • The Basics
  • Manifest Declarations
  • -
  • Using Existing Camera Apps -
      -
    1. Image capture intent
    2. -
    3. Video capture intent
    4. -
    5. Receiving camera intent result
    6. -
    +
  • Using Existing Camera Apps
  • Building a Camera App
    1. Detecting camera hardware
    2. @@ -72,7 +67,7 @@ manifest.
    3. Quick Picture or Customized Camera - How will your application use the camera? Are you just interested in snapping a quick picture or video clip, or will your application provide a new way to use cameras? For a getting a quick snap or clip, consider -Using Existing Camera Apps. For developing a customized camera feature, check +Using Existing Camera Apps. For developing a customized camera feature, check out the Building a Camera App section.
    4. Storage - Are the images or videos your application generates intended to be @@ -122,8 +117,9 @@ camera.
       <uses-permission android:name="android.permission.CAMERA" />
       
      -

      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.

    5. Camera Features - Your application must also declare use of camera features, for example: @@ -169,193 +165,17 @@ information, you must request location permission: -

      Using Existing Camera Apps

      +

      Using Existing Camera Apps

      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:

      - -
        -
      1. Compose a Camera Intent - Create an {@link android.content.Intent} that -requests an image or video, using one of these intent types: -
          -
        • {@link android.provider.MediaStore#ACTION_IMAGE_CAPTURE MediaStore.ACTION_IMAGE_CAPTURE} - -Intent action type for requesting an image from an existing camera application.
        • -
        • {@link android.provider.MediaStore#ACTION_VIDEO_CAPTURE MediaStore.ACTION_VIDEO_CAPTURE} - -Intent action type for requesting a video from an existing camera application.
        • -
        -
      2. -
      3. Start the Camera Intent - Use the {@link -android.app.Activity#startActivityForResult(android.content.Intent, int) startActivityForResult()} -method to execute the camera intent. After you start the intent, the Camera application user -interface appears on the device screen and the user can take a picture or video.
      4. -
      5. Receive the Intent Result - Set up an {@link -android.app.Activity#onActivityResult(int, int, android.content.Intent) onActivityResult()} method -in your application to receive the callback and data from the camera intent. When the user -finishes taking a picture or video (or cancels the operation), the system calls this method.
      6. -
      - - -

      Image capture intent

      -

      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:

      - -
        -
      • {@link android.provider.MediaStore#EXTRA_OUTPUT MediaStore.EXTRA_OUTPUT} - This setting -requires a {@link android.net.Uri} object specifying a path and file name where you'd like to -save the picture. This setting is optional but strongly recommended. If you do not specify this -value, the camera application saves the requested picture in the default location with a default -name, specified in the returned intent's {@link android.content.Intent#getData() Intent.getData()} -field.
      • -
      - -

      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.

      - -
      -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);
      -}
      -
      - -

      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.

      - - -

      Video capture intent

      -

      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:

      - -
        -
      • {@link android.provider.MediaStore#EXTRA_OUTPUT MediaStore.EXTRA_OUTPUT} - This setting -requires a {@link android.net.Uri} specifying a path and file name where you'd like to save the -video. This setting is optional but strongly recommended. If you do not specify this value, the -Camera application saves the requested video in the default location with a default name, specified -in the returned intent's {@link android.content.Intent#getData() Intent.getData()} field.
      • -
      • {@link android.provider.MediaStore#EXTRA_VIDEO_QUALITY MediaStore.EXTRA_VIDEO_QUALITY} - -This value can be 0 for lowest quality and smallest file size or 1 for highest quality and -larger file size.
      • -
      • {@link android.provider.MediaStore#EXTRA_DURATION_LIMIT MediaStore.EXTRA_DURATION_LIMIT} - -Set this value to limit the length, in seconds, of the video being captured.
      • -
      • {@link android.provider.MediaStore#EXTRA_SIZE_LIMIT MediaStore.EXTRA_SIZE_LIMIT} - -Set this value to limit the file size, in bytes, of the video being captured. -
      • -
      - -

      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.

      - -
      -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);
      -}
      -
      - -

      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.

      - -

      Receiving camera intent result

      -

      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.

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

      Once your activity receives a successful result, the captured image or video is available in the -specified location for your application to access.

      - - +is to use an {@link android.content.Intent} to invoke an existing Android camera application. +The details are described in the training lessons +Taking Photos Simply and +Recording Videos Simply.

      Building a Camera App

      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

      Accessing cameras

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

      +are using an intent to access the camera).

      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: