As with the sensors, the camera also has a ready-made interface. The appropriate classes and methods have been created, just use them. We will start with the simplest program that makes a photo and displays it on an ImageView component. Stick one button and one imageView on the main activity:

When you press this button, the built-in image capture program will start, then take the captured image and display it on the ImageView. Below I am pasting the activity code associated with this screen. On lines 26-34 I program the button press event. The part that is related to the shooting itself is lines 30 and 31. I create a new intention to call the built-in image capture program (line 30). In the next line I start this intention. Calling such an intention looks a little different from what we have become accustomed to. We typically call the startActivity (Intent i) method, and startActivityForResult (Intent and int x) appears here. The method itself assumes that the called intention will give us something. At the end of the operation of the intent and return of the data, the onActivityResult method is called (about this in a moment). As the parameters indicate the intent to be run and the call identifier. The second parameter specifies a value (an int parameter) that will be returned after taking a picture. Useful if we called a few different intentions, expecting different results. Each of these intentions will call the onActivityResult method, which we can later implement later on, after which we can derive which intial / return statement was returned. Useful if we called a few different intentions, expecting different results. Each of these intentions will call the onActivityResult method, which we can later implement later on, after which we can derive which intial / return statement was returned. Useful if we called a few different intentions, expecting different results. Each of these intentions will call the onActivityResult method, which we can later implement later on, after which we can derive which initial / return statement was returned.


In lines 38-44 I am implementing the onActivityResult method, which is automatically invoked when our invocation using the startActivityForResult method ends. In this method, we take the picture taken. Let’s look at the conditions on line 39. I’m checking the requestCode parameter. This is the parameter that I passed when calling intent (line 31). I’m checking to see if requestCode is equal to 1, to make sure it’s calling the intent of taking a picture (I could have called several different intentions earlier and expected the result). The second parameter – resultCode is returned by the called activity – here the built-in image capture program. RESULT_OK is returned when the photo is approved. It can also be returned RESULT_CANCEL if someone cancels taking a photo. On lines 40 and 41 I take a picture taken. To collect a photo I always have to refer to the “data” tag. Line 42 simply sets the received picture as a picture for the ImageView component. Using the camera requires appropriate permissions, so we add them to the manifest (AndroidManifest.xml) of the application in this way:


<uses-permission android: name = “android.permission.CAMERA”  />


When you start and press the button, we have a built-in photo editor:


I have forgotten the “peck” to do, I would put on “sidd” inserted “salty focia”;). After taking a photo, the bottom panel should be replaced with:

I can cancel the operation here, repeat the photo, or approve. We approve and after returning from this activity our screen looks like this:


Except for pictures, you will probably see something nicer: p Using this method, we can only take small photos. In a moment we will do it a little differently and we will get a full picture.

I created a new application. To make a full-size photo, Android will have to save it in a file. As before, I’m adding a request for the camera’s manifest file permission, but now I also need to get permission to use a memory card or some other folder.

<uses-permission android: name = “android.permission.CAMERA”  />

<uses-permission android: name = “android.permission.WRITE_EXTERNAL_STORAGE”  />


Android has a publicly available directory (available for all apps). In order to find one, we can use such a construction:

File Directory = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES);

As always, I’m starting to work with sticky components:


I gave one the button that will launch the intention and one TextView on which to find the path to the photograph taken.

In the onCreate method, I’m attaching a component reference, and as a push button operator, I call the Gook () method.

The method “SupportGuide” is described below:

I explain things that I have not discussed before. On line 50 I create a file that I will use in a moment to save the photo to disk. Lines 51-54 only serve to name the file so that the name contains the date and time of the photo. If you want, you can simply call it a “photo”. Line 57 is a reference to the directory for the images. On line 59, I create the file where the image will be located. In line 63, I give an additional parameter, ie the path to the file where the image is to be found. At the end of the intention cycle, I display the path to the generated file on the textView.



We may not be responsible for the fact that we need to click on the Android activity to take pictures. We would like, for example, the program itself took a picture in response to an event – eg motion sensor activation. The need for additional human clicks in this case is not an issue. It is possible to automatically take pictures without calling for additional system activity, but it requires more work.

I created a new project and glued to the button and imageView:


The premise is that as soon as the button is clicked, a photo will be taken and displayed on the imageView. I also have to take care of the required permission:

<uses-permission android: name = “android.permission.CAMERA” />


We are going to code now.

In order to automatically receive a picture, we need to implement PictureCallback (line 26) in the class. This will cause the onPictureTaken method to be passed to the image to be passed as a byte array. This method will take us a few moments. Lines 37,38 are references to components, nothing extraordinary. The code from line 41-51 is to check if the device has a camera at all (line 41) and find its identifier and start up. On line 45, the cameraId number assigned to the cameraId is returned by the find method. It returns a value greater than -1 if the rear camera is found, if not found – the method returns -1. I hope it explains the if condition from line 46. The code of the method itself will be taken care of in a moment. Line 49 is the start of the camera for the identifier returned by the find method. Let’s move on:

Lines 53-61 is the call on my onC method at the click of a button. In lines 64-67 we will find a method that takes a picture by the camera. It is important from the functional point of view to call the takePicture method for a Camera object for which we have attached a device identifier (line 49) earlier. Lines 70 – 81 is a method that returns the identifier of the rear camera. We called it earlier (line 45) to find the camera ID. The principle of operation is simple. There is a loop whose content is executed as many times as there are cameras in the system. For each “turn” I check whether the camera being tested is the one that corresponds to the camera rear view ID (CameraInfo.CAMERA_FACING_BACK on line 75). If we wanted to take pictures with the front camera, We can use the CAMERA_FACING_FRONT identifier. Let’s move on to the rest of the code:

Lines 85-90 will unlock the camera in case of “minimizing” the program so that other programs can also use this device. I hereby pass the default method onPause Activity class. Lines 94-97 is an implementation of the onPictureTaken method (we need to implement a PictureCallBack interface.) This method is automatically called when the image is taken. 96 is the view of the photos on the component of the ImageView class.And what is the most important thing? The most important elements:

  • We implement a PictureCallback interface (line 26)
  • Device connection under camera class (line 45)
  • Calling the takePicture method of a Camera object when we want to take a picture (line 65)
  • Implementation of the automatically invoked onPictureTaken method and processing of the passed to us by the image argument (line 94).

These elements are our “engine”, the rest are secondary accessories.

Effect after launching and clicking the button.