錄影
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
注意:本頁面所述是指已淘汰的 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 相機應用程式會透過傳送至 onActivityResult()
的 Intent
,以 Uri
形式傳回影片,這個 URI 會指向影片在儲存空間的位置。下列程式碼會擷取這個影片,並在 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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間: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```"]]