Android working with Camera

Android working with Camera

Today we are going to learn how to capture image via Camera in Android.

Integrating camera can be done in two ways. One is to use android inbuilt camera app which is very easy process. This method won’t give you much control over camera as everything is taken care by inbuilt camera app. This way will be appropriate when your app need just a picture or video from camera.

Second way is to build a custom camera interface and adding the functionality. This needs higher effort as we have to do everything by our own. Building custom camera interface will be useful when you are building an app which mainly deals with images and videos like WhatsApp.

Prerequisite:

  1. Marshmallow Permission.
  2. Activity with Result.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. These are some strings that we are going to use in our project.

3. Now open activity_main.xml and all the below layout code to it. It contains one Button to trigger capture image and an ImageView to display captured image.

4. Open your AndroidManifest.xml and add the below permissions into it. This permissions will require to Capture Image.

5. Now open your MainActivity.java and follow the below steps to implement capture image functionality:

    •  Take Picture

 

 

  • Get Result in onActivityResult : You will get captured image in onActivityResult method.

  • Displaying Captured Image : Display captured image by converting into Bitmap. Helper methods are in next point.

  • Handle Uri NullPointer Exception 

Full Code:

6. Create new java class naming CameraUtils.java, this class contains helper methods for camera.

7. For Lollipop+ Devices we will get the actual image path from Uri with the help of below class naming ImagePath_Marshmallow.java. We have to pass the Uri and it will return image path.

8. Now, you are all done and you can also add the Camera functionality to your app.

Thanks. :)

Download Source Code

 

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

COMMENTS (2)
Reply

Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 clip={text/uri-list U:file:///storage/emulated/0/Android/data/com.camera_demo/files/Pictures/Camera/IMG_20180402_124437.jpg} (has extras) }

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)