Published in


Create an Android Camera App using Kotlin

Figure 1: Photo by Robert Shunev on Unsplash

The Android Framework provides us with support for taking images and videos using the built-in camera app. But what if you want to develop your own without writing to much boilerplate code?

Today we are going to do exactly that using the Fotoapparat library which provides an easy way to interact with the Android camera while still maintaining a powerful collection of parameter customization options.

The project we will develop

We will create a simple camera app which lets the user take photos, switch from the back to the front camera and vice versa and control the flash. It will also provide the user with a preview of the camera using the CameraView which is included in the Fotoapparat library.

So, without wasting any further time, let’s get started.

Importing the dependencies

The first thing we need to do is importing the needed dependencies for our app. We can do so by adding a few lines of code to our build.gradle (Module:app) file.

Creating the UI

Now, we need to build the user interface of our camera app. It consists of a simple layout with 3 FloatingActionButtons which will be used to take an image, switch cameras and control the flash. We will also include a CameraView which will display a preview of the camera.

Requesting the required permissions

After creating the UI we can almost start using the Fotoapparat to build our app. But first, we need to request the required permissions to use the camera and access the local storage. We can do that with some simple lines of code in our AndroidManifest.xml file.

We also need to check if the user has really enabled the permissions and request them at runtime if he hasn’t. We can do so in our MainActivity.kt file.

Taking an image

Adding OnClickListeners

First, we need to add OnClickListeners to the buttons to make sure they react to user events.

Configuring the Fotoapparat instance

Next, we need to configure our instance of the Fotoapparat class.

Here we initialize the Fotoapparat instance and define basic parameters like which camera and scale-type to use. We also define the view in which the camera preview will be displayed. After that we start the preview in the onStart() method and stop it in the OnStop().

Taking an image

Next, we need to define a path and filename for our output and start implementing the image taking functionality.

Here we check if we have all the required permission. If not we request the remaining ones. After we have all the right permission we take an image and save it in the local storage path we defined above.

Switching cameras

Managing States

Now we can start managing the states of our Fotoapparat to make sure we have all the information needed for performing camera events like switching from the back to the front camera.

Note: The state of the Fotoapparat is only used to make sure that the activity is reloaded when we start the app and request the permissions for the first time so the user sees the preview.

Here we use enum classes to manage our states to make sure we know exactly what we should do when the switchCamera or the changeFlashState methods are called.

Switching Cameras

After checking the state we can continue working on our camera features.

Here we implemented the switch camera functionality using the state we defined above and the switchTo function provided by the Fotoapparat library. To switch the camera, we need to pass the camera we want and our camera configuration (We pass an empty instance for the standard configuration).

Turning the flash on

The changeFlashState method is very similar to the switchCamera one. We are working with the flashState we defined above and just invert it when the changeFlashState method is called.

After that, we change the flashState to match the current flash condition.

Complete Source Code for the MainActivity.kt

Here you can get the complete source code for our basic camera app:

Looking up the image

After taking an image you need to go into the local storage of your Android device to look at it. I’m just including it for the people who aren’t confident navigating in the local Android storage.

  1. Open the files app on your Android device
  2. Go to the local register
  3. Search for test.png

Recommended Reading

Closing Notes

So, by now you should have a pretty good idea how you can develop your own camera app using the Fotoapparat library. You have also got a good idea of how to request permission in the runtime.

The full source code of this project can be found on GitHub.

If you have found this useful, please consider recommending and sharing it with other fellow developers.

Originally published at



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store