Coding Paradox

Blog On .Net, CSharp, Windows Phone Programming

Using Camera in Android Application

without comments

In this post we will take a look at how to integrate camera with our android application. There are two ways in which we can use Camera in our Android application. Android provides you with 2 options while working with Camera

1) Use an existing Camera application

2) Work with Camera hardware API directly

If you are developing a full fledged camera application, then you need to work with the Camera API directly as it gives you more control but it involves a lot more coding. If you just want to integrate the camera with your app, the first option is better and simpler. In this tutorial, we will learn how to use an existing camera app with our application. We will create a simple application that will take pictures and display the same in a ListView. The application will display a Camera button, clicking which will open the existing camera app present on the device. Once the user takes a picture, he will be redirected back to our application and the picture that he has taken will be displayed in a listview and it will be saved to a permanent location.

Getting Started

I am using Eclipse for development however the steps will be similar for other software as well

Open Eclipse -> Create a New Android Application project called “CameraAppDemo” with the options as selected below and leave the rest of options as default.

Note: I am currently using Android API 19 but you can target the latest version if you have that installed.

Since we will be displaying the result in a listview, we will add a list view to the activity_main.xml file in the layout folder.

Since our main layout contains a list view, it is better to extend our MainActivity class from “ListActivity” instead of Activity as it will be simpler to handle listview events.

To take a picture, we will display the camera icon in the ActionBar. Modify the main.xml file in the res/menu directory as shown below

You can find the “ic_action_camera” icon inside the drawable folders in the attached solution. On click of the Camera button, the following functions are called

On click of camera button, we call the “StartCapture” function which defines an intent with “android.provider.MediaStore.ACTION_IMAGE_CAPTURE” which specifies that we want to capture an image. This will start the default camera application present on the device.

In the “CreateImageFile()” function, we create an image file which will store the actual image which has been captured. In that method, we first create a file name using the current timestamp. We then specify the directory using the “getExternalFilesDir()“. This method returns a directory which is private to our app. If you want to store the images in the public images directory, you can use the “Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);” method.

We then pass this extra info along with the intent using “cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));“. This will save the captured image in the following path which we have passed. We then start the activity passing the Intent using “startActivityForResult(cameraIntent, CAMERA_CAPTURE);” . The “CAMERA_CAPTURE” is a  integer value which i have used which can be any value.

This will start the camera application. Once you capture an image and confirm, the code will return back to the application and we will handle it in the onActivityResult() method as shown below

In the function, we check for requestCode and then check if the resultCode is OK. If that is the case, we then call the DisplayImages() function which displays all the captured images in the ListView.

In the “DisplayImages()” function, we again get the path to the storage directory, iterate through the directory and add the AbsolutePath() of all the images in an Arraylist. We then pass that to the CustomAdapter which we have created.

Here is the code for out CustomAdapter layout file.

We are using the above layout in our custom adapter for displaying the image along with the file name.

In the adapter, we are passing an ArrayList containing the path of all the files. In the “GetView()” method, we read the Bitmap from the file location and then display the same in the imageview along with the name of the file.

Now lets write some code that will display the selected image in a popup window when click on any image in the main ListView. Lets create a custom Dialog for the same. Add a new layout file in the res/layout  called “cust_dialog.xml“. This will be the layout of our Dialog. The code for which is shown below

Our custom layout contains an imageview displaying the selected image and a button which will close the dialog.

We will now handle the ListItem Click event in our MainActivity as  shown below

In the above code, the position will gives us the position of the element which was clicked. We get the absolute path of the selected image from our ‘listOfImages‘ arraylist. We then create the dialog from our custom layout and assign the image in the same.

Below is our complete code

Below is the screenshot of how our application will behave when running on emulator


Download Solution

Written by Mako

December 2nd, 2014 at 8:57 pm

Posted in Android

Tagged with ,