Android Studio : How to detect camera, activate and capture example
Let's have some fun with Android by learning how to detect the camera, activate the camera and capture with the camera. This app has one button. When pressed, first it will detect if the device has a camera or not. If the device has a camera, it will activate the camera and launch the default Android camera app to capture from the camera.
NOTE: In our example, the camera view is simulated by the Android Virtual Device. You can transfer the APK file over to a real Android device to see how this application works with a real camera.
Here you go!
Fill the XML layout design in the main_activity_camera.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sweetlogic.camera.MainCamera"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<Button
android:id="@+id/buttonActivateCamera"
android:layout_width="300dp"
android:layout_height="50dp"
android:text="Activate Camera"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="42dp"
tools:layout_editor_absoluteY="230dp" />
</android.support.constraint.ConstraintLayout>
Next step is to fill in the Java codes at MainCamera.java
package com.example.sweetlogic.camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Context;
import android.content.pm.PackageManager;
import android.widget.Toast;
import android.util.Log;
import android.provider.MediaStore;
import android.content.Intent;
public class MainCamera extends AppCompatActivity {
// global variable
boolean cameraAvailable = false;
static final int REQUEST_IMAGE_CAPTURE = 1;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_camera);
// listen to activate camera button
addListenerOnActiveCameraButton();
}
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(MainCamera.this, "this device has a camera", Toast.LENGTH_LONG).show();
return true;
} else {
Toast.makeText(MainCamera.this, "no camera on this device", Toast.LENGTH_LONG).show();
return false;
}
}
public void addListenerOnActiveCameraButton() {
Button button = (Button) findViewById(R.id.buttonActivateCamera);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cameraAvailable = checkCameraHardware(context);
if (cameraAvailable) {
// ok, we will use Intent to take photo
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
Log.i("camera", "yes");
} else {
Log.i("camera", "no");
}
}
});
}
}
Hit the Run
button and press the button on your application to activate the camera.
To see your application log messages, on the lower-left corner of the Android Studio, press the Android Monitor
tab and click on the logcat
tab.
References:
https://www.socketloop.com/tutorials/android-studio-image-button-and-button-example
https://developer.android.com/guide/topics/media/camera.html#detect-camera
https://developer.android.com/training/camera/photobasics.html
See also : Android Studio : AlertDialog to get user attention example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+28.2k Get file path of temporary file in Go
+18.3k Golang : Implement getters and setters
+16.7k Golang : Get input from keyboard
+19.7k Golang : Determine if directory is empty with os.File.Readdir() function
+17.2k Golang : Find smallest number in array
+7.3k Golang : Handling Yes No Quit query input
+7.6k Swift : Convert (cast) String to Double
+6.4k Golang : How to validate ISBN?
+9k Golang : Temperatures conversion example
+13.9k Elastic Search : Mapping date format and sort by date
+16.8k Golang : Find file size(disk usage) with filepath.Walk
+14.9k Golang : Accurate and reliable decimal calculations