Quay video
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Lưu ý: Trang này đề cập đến lớp Camera (không dùng nữa). Bạn nên dùng CameraX hoặc Camera2 (trong một số trường hợp sử dụng cụ thể). Cả CameraX và Camera2 đều hỗ trợ Android 5.0 (API cấp 21) trở lên.
Bài học này giải thích cách quay video bằng các ứng dụng máy ảnh hiện có.
Ứng dụng của bạn có việc cần làm và việc tích hợp video chỉ là một phần nhỏ của việc đó. Bạn muốn quay video mà không tốn nhiều công sức và không phải đổi mới máy quay video. Thật may là hầu hết thiết bị chạy Android đều có ứng dụng máy ảnh kết hợp chức năng quay video. Trong bài học này, bạn sẽ tìm hiểu cách dùng ứng dụng máy ảnh để quay video.
Hãy tham khảo các tài nguyên liên quan sau:
Yêu cầu tính năng máy ảnh
Để quảng cáo rằng ứng dụng này phụ thuộc vào việc có máy ảnh, hãy đặt thẻ <uses-feature>
vào tệp kê khai:
<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" />
...
</manifest>
Nếu ứng dụng của bạn dùng máy ảnh nhưng không cần phải có máy ảnh mới hoạt động được, hãy đặt android:required
thành false
. Khi làm như vậy, Google Play sẽ cho phép các thiết bị không có máy ảnh tải ứng dụng của bạn xuống. Sau đó, bạn có trách nhiệm kiểm tra khả năng hoạt động của máy ảnh trong thời gian chạy bằng cách gọi hasSystemFeature(PackageManager.FEATURE_CAMERA)
.
Nếu máy ảnh không sử dụng được, bạn nên tắt các tính năng của máy ảnh.
Xem video
Ứng dụng Máy ảnh trên Android trả về video trong Intent
được phân phối cho onActivityResult()
dưới dạng Uri
trỏ đến vị trí video trong bộ nhớ. Mã sau đây truy xuất và hiển thị video này trong 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);
}
}
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[],[],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```"]]