Intent kamera
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Untuk melakukan tindakan kamera dasar seperti mengambil foto atau merekam video dengan aplikasi kamera default perangkat, Anda tidak perlu berintegrasi dengan library Camera. Sebagai gantinya, gunakan Intent
.
Mengambil foto dengan aplikasi kamera
Android mendelegasikan tindakan ke aplikasi lain dengan memanggil Intent
. Proses ini terdiri dari tiga bagian: Intent
itu sendiri, panggilan untuk memulai Activity
eksternal, dan kode untuk menangani data gambar saat fokus beralih kembali ke aktivitas Anda.
Berikut adalah fungsi yang memanggil Intent
untuk mengambil foto.
Kotlin
val REQUEST_IMAGE_CAPTURE = 1
private fun dispatchTakePictureIntent() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}
}
Java
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
} catch (ActivityNotFoundException e) {
// display error state to the user
}
}
Merekam video dengan aplikasi kamera
Anda juga dapat memanggil Intent
untuk merekam video.
Kotlin
val REQUEST_VIDEO_CAPTURE = 1
private fun dispatchTakeVideoIntent() {
Intent(MediaStore.ACTION_VIDEO_CAPTURE).also { takeVideoIntent ->
takeVideoIntent.resolveActivity(packageManager)?.also {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE)
} ?: run {
//display error state to the user
}
}
}
Java
static final int REQUEST_VIDEO_CAPTURE = 1;
private void dispatchTakeVideoIntent() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
else {
//display error state to the user
}
}
Metode startActivityForResult()
dilindungi oleh kondisi yang memanggil resolveActivity()
, yang menampilkan komponen aktivitas pertama yang dapat menangani Intent
. Lakukan pemeriksaan ini untuk memastikan bahwa Anda memanggil Intent
yang tidak akan membuat aplikasi Anda error.
Referensi Lainnya
Untuk tindakan kamera dasar, gunakan Intent
. Jika tidak, sebaiknya gunakan library Camera2 dan CameraX untuk hal yang lebih kompleks daripada pengambilan gambar atau perekaman video dasar.
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-08-27 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-27 UTC."],[],[],null,["# Camera intents\n\nTo perform basic camera actions like capturing a photo or video using the device's default camera application, you do not need to integrate with a [Camera library](/training/camera/choose-camera-library). Instead, use an [`Intent`](https://developer.android.com/reference/android/content/Intent).\n\nTake a photo with a camera app\n------------------------------\n\nAndroid delegates actions to other applications by invoking an `Intent`. This process involves three pieces: the `Intent` itself, a call to start the external `Activity`, and some code to handle the image data when focus returns to your activity.\n\nHere's a function that invokes an `Intent` to capture a photo. \n\n### Kotlin\n\n```kotlin\nval REQUEST_IMAGE_CAPTURE = 1\n \nprivate fun dispatchTakePictureIntent() {\n val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)\n try {\n startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)\n } catch (e: ActivityNotFoundException) {\n // display error state to the user\n }\n}\n```\n\n### Java\n\n```java\nstatic final int REQUEST_IMAGE_CAPTURE = 1;\n \nprivate void dispatchTakePictureIntent() {\n Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);\n try {\n startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);\n } catch (ActivityNotFoundException e) {\n // display error state to the user\n }\n}\n```\n\nRecord a video with a camera app\n--------------------------------\n\nYou can also invoke an `Intent` to capture a video. \n\n### Kotlin\n\n```kotlin\nval REQUEST_VIDEO_CAPTURE = 1\n \nprivate fun dispatchTakeVideoIntent() {\n Intent(MediaStore.ACTION_VIDEO_CAPTURE).also { takeVideoIntent -\u003e\n takeVideoIntent.resolveActivity(packageManager)?.also {\n startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE)\n } ?: run {\n //display error state to the user\n }\n }\n}\n```\n\n### Java\n\n```java\nstatic final int REQUEST_VIDEO_CAPTURE = 1;\n \nprivate void dispatchTakeVideoIntent() {\n Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);\n if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {\n startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);\n }\n else {\n //display error state to the user\n }\n}\n```\n\nThe `startActivityForResult()` method is protected by a condition that calls `resolveActivity()`, which returns the first activity component that can handle the `Intent`. Perform this check to ensure that you are invoking an `Intent` that won't crash your app.\n\nAdditional Resources\n--------------------\n\nFor basic camera actions, use an `Intent`. Otherwise, it is recommended to use the Camera2 and CameraX libraries for anything more complex than basic image or video capture.\n\n- [CameraX camera package](https://developer.android.com/training/camerax)\n- [Camera2 camera package](https://developer.android.com/training/camera2)\n- [Camera sample projects](https://github.com/android/camera-samples)"]]