Skip to content

Android-Dev-Hub/Android-Camera-Intent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Android Camera App

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.


Features

  • Open the camera from inside the app
  • Capture an image and store it temporarily
  • Display the captured image inside the app
  • Uses FileProvider for secure file sharing
  • Glide library for efficient image loading

Screenshots

App Screenshot


Prerequisites

  • Android Studio installed
  • Minimum Android SDK: 21 (Lollipop)
  • Kotlin support

Setup & Installation

  1. Clone the repository
git clone https://github.com/YourUsername/AndroidCameraApp.git
  1. Open the project in Android Studio
  2. Add dependencies in build.gradle (Module: app)
  3. dependencies {
        implementation("com.github.bumptech.glide:glide:4.16.0")
    }
  4. Update AndroidManifest.xml
  5. <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>
  6. Create file_paths.xml in res/xml
  7. <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <cache-path name="images" path="images/" />
    </paths>
  8. activity_main.xml:
  9. <?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>
  10. MainActivity.kt
  11. 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)
            }
        }
    }
  12. Run the app on a device or emulator with a camera

Usage

  1. Open the app.
  2. Tap the “Open Camera” button.
  3. Capture an image.
  4. The captured image will appear in the ImageView.

Code Snippet

Opening Camera and displaying image using Glide:

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)

Load image in ImageView:

Glide.with(this).load(photoUri).into(clickedImage)

License

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published