Android Studio Examples

100+ tutorials, source code, and examples to help you develop creative and technical skills.

How to capture image from Camera in Android Studio with Example

MainActivity.java

package com.learnoset.testproject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ImageView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements ActivityResultCallback<ActivityResult> {

    private ActivityResultLauncher<Intent> activityResultLauncher;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);

        // Register for Activity Result Launcher
        activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), this);

        // Take Photo
        dispatchTakePictureIntent();
    }


    private void dispatchTakePictureIntent() {

        // Creating Take Photo Intent to launch Camera
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        // launch camera to take photo
        activityResultLauncher.launch(takePictureIntent);
    }

    @Override
    public void onActivityResult(ActivityResult result) {

        // Getting photo data
        Bundle extras = result.getData().getExtras();

        // getting bitmap from photo data
        Bitmap imageBitmap = (Bitmap) extras.get("data");

        // setting bitmap to the ImageView
        imageView.setImageBitmap(imageBitmap);
    }
}

AndroidManifest.xml

<!--    Adding camera permissions in the AndroidManifest.xml file-->
    <uses-feature android:name="android.hardware.camera"
        android:required="true" />

Projects with Source Code + Video Tutorials

You can download our Java and Android Studio Projects with Source Code and Video Tutorials.

Login-&-Register-Screen-UI-Design---02
Login & Register Screen UI Design - 02
Gamer-Bazi---Tournament-Application-with-Admob-Ads-&-Web-Admin-Panel
Gamer Bazi - Tournament Application with Admob Ads & Web Admin Panel
Online-Tic-Tac-Toe-game-Using-Firebase-Database-in-Android-Studio
Online Tic Tac Toe game Using Firebase Database in Android Studio

If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com

Follow us to learn Coding and get in touch with new Technologies.