| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 1 | page.title=Recording Videos Simply |
| Scott Main | 580f014 | 2011-12-15 16:47:26 -0800 | [diff] [blame] | 2 | parent.title=Capturing Photos |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 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> |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 27 | <li><a href="{@docRoot}guide/components/intents-filters.html">Intents and Intent |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 28 | Filters</a></li> |
| 29 | </ul> |
| 30 | |
| 31 | <h2>Try it out</h2> |
| 32 | |
| 33 | <div class="download-box"> |
| Scott Main | 580f014 | 2011-12-15 16:47:26 -0800 | [diff] [blame] | 34 | <a href="http://developer.android.com/shareables/training/PhotoIntentActivity.zip" |
| 35 | class="button">Download the sample</a> |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 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 | |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 57 | <pre style="clear:right"> |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 58 | <manifest ... > |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 59 | <uses-feature android:name="android.hardware.camera" |
| 60 | android:required="true" /> |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 61 | ... |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 62 | </manifest> |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 63 | </pre> |
| 64 | |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 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 |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 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 | |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 73 | <h2 id="TaskCaptureIntent">Record a Video with a Camera App</h2> |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 74 | |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 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> |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 79 | |
| 80 | <p>Here's a function that invokes an intent to capture video.</p> |
| 81 | |
| 82 | <pre> |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 83 | static final int REQUEST_VIDEO_CAPTURE = 1; |
| 84 | |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 85 | private void dispatchTakeVideoIntent() { |
| 86 | Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 87 | if (takeVideoIntent.resolveActivity(getPackageManager()) != null) { |
| 88 | startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE); |
| 89 | } |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 90 | } |
| 91 | </pre> |
| 92 | |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 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> |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 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 |
| Pin Ting | 6f5b5ee | 2012-03-03 00:13:32 +0800 | [diff] [blame] | 107 | retrieves this video and displays it in a {@link android.widget.VideoView}.</p> |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 108 | |
| 109 | <pre> |
| Scott Main | 326ed25 | 2013-12-04 17:54:03 -0800 | [diff] [blame] | 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 | } |
| Scott Main | 564e8aa | 2011-12-15 15:59:34 -0800 | [diff] [blame] | 116 | } |
| 117 | </pre> |
| 118 | |