Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
To open the camera through an Intent
and display the captured image in an ImageView
, follow the steps below:
AndroidManifest.xml
:<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Starting from Android 6.0 (API level 23), you also need to request runtime permissions for the camera and storage.
In your activity_main.xml
(or equivalent layout file):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <ImageView android:id="@+id/capturedImage" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scaleType="centerCrop"/> <Button android:id="@+id/captureImageButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Capture Image" /> </LinearLayout>
MainActivity
:import android.app.Activity import android.content.Intent import android.content.pm.PackageManager import android.graphics.Bitmap import android.os.Bundle import android.provider.MediaStore import android.widget.Button import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat class MainActivity : AppCompatActivity() { companion object { const val REQUEST_IMAGE_CAPTURE = 1 const val CAMERA_PERMISSION_REQUEST_CODE = 2 } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val captureImageButton: Button = findViewById(R.id.captureImageButton) captureImageButton.setOnClickListener { // Check for permissions if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // Request permissions ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CAMERA), CAMERA_PERMISSION_REQUEST_CODE) } else { openCamera() } } } private fun openCamera() { val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) if (takePictureIntent.resolveActivity(packageManager) != null) { startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE) } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) { val imageBitmap = data?.extras?.get("data") as Bitmap val capturedImage: ImageView = findViewById(R.id.capturedImage) capturedImage.setImageBitmap(imageBitmap) } } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { when (requestCode) { CAMERA_PERMISSION_REQUEST_CODE -> { if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { openCamera() } return } } } }
This code checks for camera permissions, opens the camera, captures the image, and sets it to an ImageView
.
Ensure you handle permissions gracefully in a real-world application. This includes guiding the user on what to do if they deny a required permission.
Capture image using Intent in Android:
Use the following code to capture an image using the camera Intent:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
Remember to handle the result in onActivityResult
.
How to launch camera in Android and display captured image:
Launch the camera and display the captured image with the following:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
Handle the result in onActivityResult
to get the captured image.
Android camera Intent example with image display:
After capturing the image, use the captured data to display it:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); // Assuming imageView is your ImageView } }
Open camera and show image in Android app:
Open the camera and display the captured image using the camera Intent:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
Handle the result in onActivityResult
as shown in the previous example.
Take a picture with camera Intent and display in Android:
Use the camera Intent to take a picture and display it:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
Process the result in onActivityResult
to obtain and display the captured image.
Display captured image from camera in Android:
Display the captured image in your ImageView
:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); // Assuming imageView is your ImageView } }
Android camera Intent result and image display code:
Use the following code to handle the camera Intent result and display the captured image:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); // Assuming imageView is your ImageView } }
Ensure that you have the necessary permissions declared in your manifest to access the camera.