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 <uses-feature>} tag in the manifest file:</p> 56 57 <pre style="clear:right"> 58 <manifest ... > 59 <uses-feature android:name="android.hardware.camera" 60 android:required="true" /> 61 ... 62 </manifest> 63 </pre> 64 65 <p>If your application uses, but does not require a camera in order to function, set {@code 66 android:required} to {@code false}. In doing so, Google Play will allow devices without a 67 camera to download your application. It's then your responsibility to check for the availability 68 of the camera at runtime by calling {@link 69 android.content.pm.PackageManager#hasSystemFeature hasSystemFeature(PackageManager.FEATURE_CAMERA)}. 70 If a camera is not available, you should then disable your camera features.</p> 71 72 73 <h2 id="TaskCaptureIntent">Record a Video with a Camera App</h2> 74 75 <p>The Android way of delegating actions to other applications is to invoke an {@link 76 android.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}, 78 and some code to handle the video when focus returns to your activity.</p> 79 80 <p>Here's a function that invokes an intent to capture video.</p> 81 82 <pre> 83 static final int REQUEST_VIDEO_CAPTURE = 1; 84 85 private void dispatchTakeVideoIntent() { 86 Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 87 if (takeVideoIntent.resolveActivity(getPackageManager()) != null) { 88 startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE); 89 } 90 } 91 </pre> 92 93 <p>Notice that the {@link android.app.Activity#startActivityForResult 94 startActivityForResult()} method is protected by a condition that calls 95 {@link android.content.Intent#resolveActivity resolveActivity()}, which returns the 96 first activity component that can handle the intent. Performing this check 97 is important because if you call {@link android.app.Activity#startActivityForResult 98 startActivityForResult()} using an intent that no app can handle, 99 your app will crash. So as long as the result is not null, it's safe to use the intent. </p> 100 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 105 to {@link android.app.Activity#onActivityResult onActivityResult()} as a {@link 106 android.net.Uri} pointing to the video location in storage. The following code 107 retrieves this video and displays it in a {@link android.widget.VideoView}.</p> 108 109 <pre> 110 @Override 111 protected 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 } 116 } 117 </pre> 118 119