Accessing Camera and Taking Pictures in Android

In today’s world of selfies, most of the app you develop would need access to the camera. In this tutorial, we will see how can we capture a picture using the device camera, save it to the storage and preview it.

Creating the Project and Adding Dependencies

1. Create a new Project in Android Studio named Access Camera.

2. Open AndroidManifest.xml add the permissions to access camera and external storage. Also, add features android.hardware.camera and android.hardware.camera.autofocus. Additionally, we need to configure the FileProvider as highlighted below:

3. Now, we need to create the XML resource mentioned in the above provider. Create a folder named xml inside the res folder and create a file named file_paths.xml, inside the newly created folder and add the following content:

4. Open strings.xml in res/values folder and add the following values:

5. We will be using Easy Permissions library for handling runtime permissions in Android M and above. So add the dependency to build.gradle file of the app module:

Updating activity_main.xml

6. Now open activity_main.xml and add an ImageView to preview the captured image and a Button, on click of which the camera is launched.

 

Updating the MainActivity.java

7. Update MainActivity.java with the following code. I have added necessary comments in the code for better understanding:

Demonstration

In the below animation you can find the demo of the app:

Access Camera Demo

Source Code and APK Files

You can download the Source code and APK files from the below links:

Download Source CodeDownload APK

If you have any doubts, then we can discuss them in the comments section. If you have liked this tutorial, please do follow us on Twitter and Facebook.

Stay Connected

No spam guarantee.

Abhishek

Abhishek

Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.
Abhishek

Leave a Reply

Your email address will not be published. Required fields are marked *