Home | History | Annotate | Download | only in camera
      1 page.title=Recording Videos Simply
      2 parent.title=Capturing Photos
      3 parent.link=index.html
      4 
      5 trainingnavtop=true
      6 previous.title=Recording Photos Simply
      7 previous.link=photobasics.html
      8 next.title=Controlling the Camera
      9 next.link=cameradirect.html
     10 
     11 @jd:body
     12 
     13 
     14 <div id="tb-wrapper">
     15   <div id="tb">
     16 
     17     <h2>This lesson teaches you to</h2>
     18     <ol>
     19       <li><a href="#TaskManifest">Request Camera Permission</a></li>
     20       <li><a href="#TaskCaptureIntent">Record a Video with a Camera App</a>
     21       <li><a href="#TaskVideoView">View the Video</a></li>
     22     </ol>
     23 
     24     <h2>You should also read</h2>
     25       <ul>
     26         <li><a href="{@docRoot}guide/topics/media/camera.html">Camera</a></li>
     27         <li><a href="{@docRoot}guide/components/intents-filters.html">Intents and Intent 
     28       Filters</a></li>
     29       </ul>
     30 
     31     <h2>Try it out</h2>
     32 
     33     <div class="download-box">
     34       <a href="http://developer.android.com/shareables/training/PhotoIntentActivity.zip"
     35 class="button">Download the sample</a>
     36       <p class="filename">PhotoIntentActivity.zip</p>
     37     </div>
     38   </div>
     39 </div>
     40 
     41 
     42 <p>This lesson explains how to capture video using existing camera
     43 applications.</p>
     44 
     45 <p>Your application has a job to do, and integrating videos is only a small
     46 part of it.  You want to take videos with minimal fuss, and not reinvent the
     47 camcorder. Happily, most Android-powered devices already have a camera application that
     48 records video. In this lesson, you make it do this for you.</p>
     49 
     50 
     51 
     52 <h2 id="TaskManifest">Request Camera Permission</h2>
     53 
     54 <p>To advertise that your application depends on having a camera, put a
     55 {@code &lt;uses-feature&gt;} tag in the manifest file:</p>
     56 
     57 <pre>
     58 &lt;manifest ... >
     59     &lt;uses-feature android:name="android.hardware.camera" /&gt;
     60     ...
     61 &lt;/manifest ... >
     62 </pre>
     63 
     64 <p>If your application uses, but does not require a camera in order to function, add {@code
     65 android:required="false"} to the tag. In doing so, Google Play will allow devices without a
     66 camera to download your application. It's then your responsibility to check for the availability
     67 of the camera at runtime by calling {@link
     68 android.content.pm.PackageManager#hasSystemFeature hasSystemFeature(PackageManager.FEATURE_CAMERA)}.
     69 If a camera is not available, you should then disable your camera features.</p>
     70 
     71 
     72 <h2 id="TaskCaptureIntent">Record a Video with a Camera App</h2> 
     73 
     74 <p>The Android way of delegating actions to other applications is to invoke
     75 an {@link android.content.Intent} that describes what you want done.  This
     76 involves three pieces: the {@link android.content.Intent} itself, a call to start the external
     77 {@link android.app.Activity}, and some code to handle the video when focus returns
     78 to your activity.</p>
     79 
     80 <p>Here's a function that invokes an intent to capture video.</p>
     81 
     82 <pre>
     83 private void dispatchTakeVideoIntent() {
     84     Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
     85     startActivityForResult(takeVideoIntent, ACTION_TAKE_VIDEO);
     86 }
     87 </pre>
     88 
     89 
     90 <p>It's a good idea to make sure an app exists to handle your intent
     91 before invoking it. Here's a function that checks for apps that can handle your intent:</p>
     92 
     93 <pre>
     94 public static boolean isIntentAvailable(Context context, String action) {
     95     final PackageManager packageManager = context.getPackageManager();
     96     final Intent intent = new Intent(action);
     97     List&lt;ResolveInfo> list =
     98         packageManager.queryIntentActivities(intent,
     99             PackageManager.MATCH_DEFAULT_ONLY);
    100     return list.size() > 0;
    101 }
    102 </pre>
    103 
    104 
    105 <h2 id="TaskVideoView">View the Video</h2>
    106 
    107 <p>The Android Camera application returns the video in the {@link android.content.Intent} delivered
    108 to {@link android.app.Activity#onActivityResult onActivityResult()} as a {@link
    109 android.net.Uri} pointing to the video location in storage. The following code
    110 retrieves this video and displays it in a {@link android.widget.VideoView}.</p>
    111 
    112 <pre>
    113 private void handleCameraVideo(Intent intent) {
    114     mVideoUri = intent.getData();
    115     mVideoView.setVideoURI(mVideoUri);
    116 }
    117 </pre>
    118 
    119