blob: 135d4a12ed0ccd50cd02727a8c411174faea2a77 [file] [log] [blame]
Scott Main564e8aa2011-12-15 15:59:34 -08001page.title=Recording Videos Simply
Scott Main580f0142011-12-15 16:47:26 -08002parent.title=Capturing Photos
Scott Main564e8aa2011-12-15 15:59:34 -08003parent.link=index.html
4
5trainingnavtop=true
6previous.title=Recording Photos Simply
7previous.link=photobasics.html
8next.title=Controlling the Camera
9next.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>
Scott Main326ed252013-12-04 17:54:03 -080027 <li><a href="{@docRoot}guide/components/intents-filters.html">Intents and Intent
Scott Main564e8aa2011-12-15 15:59:34 -080028 Filters</a></li>
29 </ul>
30
31 <h2>Try it out</h2>
32
33 <div class="download-box">
Scott Main580f0142011-12-15 16:47:26 -080034 <a href="http://developer.android.com/shareables/training/PhotoIntentActivity.zip"
35class="button">Download the sample</a>
Scott Main564e8aa2011-12-15 15:59:34 -080036 <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
43applications.</p>
44
45<p>Your application has a job to do, and integrating videos is only a small
46part of it. You want to take videos with minimal fuss, and not reinvent the
47camcorder. Happily, most Android-powered devices already have a camera application that
48records 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
Scott Main326ed252013-12-04 17:54:03 -080057<pre style="clear:right">
Scott Main564e8aa2011-12-15 15:59:34 -080058&lt;manifest ... >
Scott Main326ed252013-12-04 17:54:03 -080059 &lt;uses-feature android:name="android.hardware.camera"
60 android:required="true" /&gt;
Scott Main564e8aa2011-12-15 15:59:34 -080061 ...
Scott Main326ed252013-12-04 17:54:03 -080062&lt;/manifest>
Scott Main564e8aa2011-12-15 15:59:34 -080063</pre>
64
Scott Main326ed252013-12-04 17:54:03 -080065<p>If your application uses, but does not require a camera in order to function, set {@code
66android:required} to {@code false}. In doing so, Google Play will allow devices without a
Scott Main564e8aa2011-12-15 15:59:34 -080067camera to download your application. It's then your responsibility to check for the availability
68of the camera at runtime by calling {@link
69android.content.pm.PackageManager#hasSystemFeature hasSystemFeature(PackageManager.FEATURE_CAMERA)}.
70If a camera is not available, you should then disable your camera features.</p>
71
72
Scott Main326ed252013-12-04 17:54:03 -080073<h2 id="TaskCaptureIntent">Record a Video with a Camera App</h2>
Scott Main564e8aa2011-12-15 15:59:34 -080074
Scott Main326ed252013-12-04 17:54:03 -080075<p>The Android way of delegating actions to other applications is to invoke an {@link
76android.content.Intent} that describes what you want done. This process involves three pieces: The
77{@link android.content.Intent} itself, a call to start the external {@link android.app.Activity},
78and some code to handle the video when focus returns to your activity.</p>
Scott Main564e8aa2011-12-15 15:59:34 -080079
80<p>Here's a function that invokes an intent to capture video.</p>
81
82<pre>
Scott Main326ed252013-12-04 17:54:03 -080083static final int REQUEST_VIDEO_CAPTURE = 1;
84
Scott Main564e8aa2011-12-15 15:59:34 -080085private void dispatchTakeVideoIntent() {
86 Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Scott Main326ed252013-12-04 17:54:03 -080087 if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
88 startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
89 }
Scott Main564e8aa2011-12-15 15:59:34 -080090}
91</pre>
92
Scott Main326ed252013-12-04 17:54:03 -080093<p>Notice that the {@link android.app.Activity#startActivityForResult
94startActivityForResult()} method is protected by a condition that calls
95{@link android.content.Intent#resolveActivity resolveActivity()}, which returns the
96first activity component that can handle the intent. Performing this check
97is important because if you call {@link android.app.Activity#startActivityForResult
98startActivityForResult()} using an intent that no app can handle,
99your app will crash. So as long as the result is not null, it's safe to use the intent. </p>
Scott Main564e8aa2011-12-15 15:59:34 -0800100
101
102<h2 id="TaskVideoView">View the Video</h2>
103
104<p>The Android Camera application returns the video in the {@link android.content.Intent} delivered
105to {@link android.app.Activity#onActivityResult onActivityResult()} as a {@link
106android.net.Uri} pointing to the video location in storage. The following code
Pin Ting6f5b5ee2012-03-03 00:13:32 +0800107retrieves this video and displays it in a {@link android.widget.VideoView}.</p>
Scott Main564e8aa2011-12-15 15:59:34 -0800108
109<pre>
Scott Main326ed252013-12-04 17:54:03 -0800110&#64;Override
111protected void onActivityResult(int requestCode, int resultCode, Intent data) {
112 if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
113 Uri videoUri = intent.getData();
114 mVideoView.setVideoURI(videoUri);
115 }
Scott Main564e8aa2011-12-15 15:59:34 -0800116}
117</pre>
118