录制视频
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
注意:本页介绍的是已废弃的 Camera 类。建议您改用 CameraX;在特定的使用场景下,也可以改用 Camera2。CameraX 和 Camera2 都支持 Android 5.0(API 级别 21)及更高版本。
本课程介绍了如何使用已有相机应用拍摄视频。
您的应用要发挥功能,而集成视频只是您应用功能的一小部分。您可以轻松拍摄视频,而无需重新构建摄像机。值得高兴的是,大多数搭载 Android 的设备已经安装了一个用于录制视频的相机应用。在本课程中,您将学习如何使用相机应用录制视频。
请求相机功能
如需声明您的应用依赖于相机,请在清单文件中添加 <uses-feature>
代码:
<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" />
...
</manifest>
如果您的应用使用相机,但不需要相机也可以正常运行,应将 android:required
设为 false
。这样,Google Play 便会允许未装有相机的设备下载您的应用。因此,您必须负责通过调用 hasSystemFeature(PackageManager.FEATURE_CAMERA)
检查相机在运行时的可用性。如果相机不可用,您应停用相机功能。
观看视频
Android 相机应用会返回 Intent
(作为 Uri
传递给 onActivityResult()
,指向视频在存储设备中所处的位置)中的视频。下面的代码会检索此视频并将其显示在一个 VideoView
中。
Kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
val videoUri: Uri = intent.data
videoView.setVideoURI(videoUri)
}
}
Java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = intent.getData();
videoView.setVideoURI(videoUri);
}
}
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Record videos\n\n**Note:** This page refers to the\n[Camera](/reference/android/hardware/Camera) class, which is deprecated. We\nrecommend using [CameraX](/media/camera/camerax) or, for specific use cases,\n[Camera2](/media/camera/camera2). Both CameraX and Camera2 support Android 5.0\n(API level 21) and higher.\n\nThis lesson explains how to capture video using existing camera\napplications.\n\nYour application has a job to do, and integrating videos is only a small\npart of it. You want to take videos with minimal fuss, and not reinvent the\ncamcorder. Happily, most Android-powered devices already have a camera application that\nrecords video. In this lesson, you make it do this for you. \n\nRefer to the following related resources:\n\n- [Camera](/guide/topics/media/camera)\n- [Intents and Intent\n Filters](/guide/components/intents-filters)\n\nRequest the camera feature\n--------------------------\n\nTo advertise that your application depends on having a camera, put a\n`\u003cuses-feature\u003e` tag in the manifest file: \n\n```xml\n\u003cmanifest ... \u003e\n \u003cuses-feature android:name=\"android.hardware.camera\"\n android:required=\"true\" /\u003e\n ...\n\u003c/manifest\u003e\n```\n\nIf your application uses, but does not require a camera in order to function, set `android:required` to `false`. In doing so, Google Play will allow devices without a\ncamera to download your application. It's then your responsibility to check for the availability\nof the camera at runtime by calling [hasSystemFeature(PackageManager.FEATURE_CAMERA)](/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)).\nIf a camera is not available, you should then disable your camera features.\n\nView the video\n--------------\n\nThe Android Camera application returns the video in the [Intent](/reference/android/content/Intent) delivered\nto [onActivityResult()](/reference/android/app/Activity#onActivityResult(int, int, android.content.Intent)) as a [Uri](/reference/android/net/Uri) pointing to the video location in storage. The following code\nretrieves this video and displays it in a [VideoView](/reference/android/widget/VideoView). \n\n### Kotlin\n\n```kotlin\noverride fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent) {\n if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {\n val videoUri: Uri = intent.data\n videoView.setVideoURI(videoUri)\n }\n}\n```\n\n### Java\n\n```java\n@Override\nprotected void onActivityResult(int requestCode, int resultCode, Intent intent) {\n if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {\n Uri videoUri = intent.getData();\n videoView.setVideoURI(videoUri);\n }\n}\n```"]]