This Android application demonstrates how to open the device camera using an Intent, capture an image, and display it inside the app.
The app uses the ACTION_IMAGE_CAPTURE Intent of the MediaStore class and FileProvider to securely handle image files. Glide library is used to display images in an ImageView.
- Open the camera from inside the app
- Capture an image and store it temporarily
- Display the captured image inside the app
- Uses FileProviderfor secure file sharing
- Glide library for efficient image loading
- Android Studio installed
- Minimum Android SDK: 21 (Lollipop)
- Kotlin support
- Clone the repository
git clone https://github.com/YourUsername/AndroidCameraApp.git- Open the project in Android Studio
- Add dependencies in build.gradle (Module: app)
- Update AndroidManifest.xml
- Create file_paths.xml in res/xml
- activity_main.xml:
- MainActivity.kt
- Run the app on a device or emulator with a camera
dependencies {
    implementation("com.github.bumptech.glide:glide:4.16.0")
}<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider><?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="images" path="images/" />
</paths><?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- Camera Button -->
    <Button
        android:id="@+id/camera_open"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Camera"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="32dp" />
    <!-- ImageView for Captured Image -->
    <ImageView
        android:id="@+id/click_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/camera_open"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_margin="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import com.bumptech.glide.Glide
import java.io.File
class MainActivity : AppCompatActivity() {
    // Declare UI elements
    private lateinit var openCamera: Button
    private lateinit var clickedImage: ImageView
    // Variables to hold the file and its Uri
    private lateinit var photoFile: File
    private lateinit var photoUri: Uri
    // Unique request code for identifying the camera intent result
    private val CAMERA_REQUEST_CODE = 1001
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        openCamera = findViewById(R.id.camera_open)
        clickedImage = findViewById(R.id.click_image)
        openCamera.setOnClickListener {
            // Create a temporary image file to store the captured photo
            photoFile = createImageFile()
            // Get a content URI for the file using FileProvider
            // This allows secure sharing of the file with the camera app
            photoUri = FileProvider.getUriForFile(
                this,
                "${packageName}.fileprovider", // FileProvider authority (defined in manifest)
                photoFile
            )
            // Create an intent to launch the camera app
            val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
                // Tell the camera app where to save the image
                putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
                // Grant temporary write permission to the camera app for the URI
                addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            }
            // Start the camera app and wait for the result
            startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE)
        }
    }
    // Function to create a temporary image file in the cache directory
    private fun createImageFile(): File {
        // Create a directory inside cache to store images (if not already created)
        val imageDir = File(cacheDir, "images").apply { mkdirs() }
        // Create a temp file with prefix "captured_" and suffix ".jpg"
        return File.createTempFile("captured_", ".jpg", imageDir)
    }
    // Deprecated but still usable for handling activity results (like camera)
    @Deprecated("Deprecated in Java")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // Check if the result is from the camera and was successful
        if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
            // Load the captured image from the URI into the ImageView using Glide
            Glide.with(this).load(photoUri).into(clickedImage)
        }
    }
}- Open the app.
- Tap the “Open Camera” button.
- Capture an image.
- The captured image will appear in the ImageView.
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
    putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
    addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE)
Glide.with(this).load(photoUri).into(clickedImage)
This project is licensed under the MIT License.
If you want, I can also make a **super short, GitHub-ready version with badges, 3-4 lines of features, and screenshot**, perfect for a clean repo look.  
Do you want me to make that too?
Developer Ishan Walia
