Camera Program
Camera Program
java
CopyEdit
package com.example.mycamera;
This defines the package name of the application, which uniquely identifies it.
java
CopyEdit
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
These are the necessary imports for handling UI components (Button, ImageView),
camera capture (Intent, MediaStore), and activity results (ActivityResultLauncher,
ActivityResultContracts).
java
CopyEdit
java
CopyEdit
java
CopyEdit
@Override
java
CopyEdit
super.onCreate(savedInstanceState);
java
CopyEdit
setContentView(R.layout.activity_main);
java
CopyEdit
captureButton = findViewById(R.id.btnCaptureImage);
imageView = findViewById(R.id.capture_image);
java
CopyEdit
activityResultLauncher = registerForActivityResult(new
ActivityResultContracts.StartActivityForResult(),
result -> {
java
CopyEdit
java
CopyEdit
imageView.setImageBitmap(bitmap);
java
CopyEdit
});
Closes the lambda function for handling the result.
java
CopyEdit
captureButton.setOnClickListener(new View.OnClickListener() {
java
CopyEdit
@Override
java
CopyEdit
activityResultLauncher.launch(intent);
java
CopyEdit
});
java
CopyEdit
}
Closes the MainActivity class.
xml
CopyEdit
<uses-permission android:name="android.permission.CAMERA"/>
xml
CopyEdit
xml
CopyEdit
<application ...>
xml
CopyEdit
xml
CopyEdit
<intent-filter>
This app captures an image using the camera and displays it in an ImageView. Let me know if
you need any modifications! 🚀
4o
You said:
activityResultLauncher = registerForActivityResult(new
ActivityResultContracts.StartActivityForResult(), result ->
{ if(result.getResultCode()==RESULT_OK && result.getData() != null){ Bundle extras =
result.getData().getExtras(); Bitmap bitmap = (Bitmap) extras .get("data");
imageView.setImageBitmap(bitmap); } }); //explain this in detail
ChatGPT said:
This code block registers an activity result launcher to handle the result of an image capture
action and update the ImageView with the captured image.
Step-by-Step Breakdown
java
CopyEdit
activityResultLauncher = registerForActivityResult(new
ActivityResultContracts.StartActivityForResult(),
registerForActivityResult(...):
o This is used to start an activity and handle its result in a modern and safer way
(instead of the deprecated startActivityForResult()).
o It registers a callback that listens for results when an activity (like the camera app)
finishes execution.
new ActivityResultContracts.StartActivityForResult():
java
CopyEdit
result -> {
This lambda function is the callback that processes the result when the launched
activity (camera app) completes.
java
CopyEdit
result.getResultCode() == RESULT_OK:
o Ensures that the operation was successful (the user actually captured an image
instead of canceling the action).
result.getData() != null:
java
CopyEdit
result.getData().getExtras():
o Extracts the captured image from the Bundle using the key "data".
java
CopyEdit
imageView.setImageBitmap(bitmap);
Sets the Bitmap to the ImageView, updating the UI to display the captured image.
o If you need a full-resolution image, you must save it to storage and then retrieve
it from a file.