Android working with Camera - Androhub

Camera Banner

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. :)

 

2 Comments

sandhya
Monday, April 2nd, 2018

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) }

Dr. Droid
Monday, April 2nd, 2018

Hi Sandhya,

In which device you are running the APK?

Please check this link for more clarification : https://stackoverflow.com/questions/9026085/no-activity-found-to-handle-intent-act-android-media-action-image-capture

Thanks

Post comment

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