Python for Android Tutorial #2 – Taking a picture


This is the second post about mobile development with Python. It’s a short tutorial, just to help you to understand and learn how to work with Python for Mobile. In this post you will see how to take pictures using Python and Android(Not compatible with iOS).

Python for Android

I – About the tutorials

(If you had read another tutorial, you can go to the topic III, because I and II is the same in all tutorials)

All my next posts will be about this tutorial series. I’ll show you how to use each mobile API, like compass, camera, sensors, sound, and others. After the API, we will start to test some Python libs, as PyGame and OpenCV. I’m using Android with buildozer, but some libs are compatible with iOS too(I’ll tell you when it’s compatible), so if you test it, send a feedback if possible.

In each post, I’ll show a sample app using on of them. When I finish all the tutorial, you will be able to develop almost any app with Python. In the principle, I’m not going to show the design stuff, but I recommend you to check Kivy and PySide. After finishing theses tutorials, maybe I can start a tutorial series about Kivy, for the design, but it’s easier to find at Google 🙂

You can check the Github repo here, and if you like it, please, star it 🙂


To be able to read all the tutorials, please follow my blog or subscribe your email to get updates. Another important thing if your feedback. I’m Brazilian and trying to write in English, so if you have any problem, please, just let me know. To make the tutorials better ,try to comment always that is possible, showing your experiences, tips for a better tutorial, and this kind of stuff.

Before start, you can check this links to configure all the prerequisites:

II – Index

You will be able to get the links for all tutorials here, I’ll be updating when a new tutorial is published.

Tutorial #1 – Accelerometer

Tutorial #2 – Camera

Tutorial #3 – Kivy Designer

Tutorial #4 – Debug

Tutorial #5 – Android and iOS game with Python

Tutorial #6 – Reading and saving user data

Tutorial #7 – Use Android classes with PyJNIus

III – About this sample

Unfortunately Plyer camera still under development, so we will not be able to develop complex apps with it now. It’s only compatible with Android, and we are not able to get camera preview.

In this version, we can start the camera intent with the command take_picture and choose a place to save the photo. So the app will open the Android camera, take a picture, and if you need to, you can read this image when necessary.

To show you how to use this class, I’ll show a simple sample, when the user click in the screen, a new image will be taken. A label will show where the image was saved. So if you are using Kivy or PySide for the UI, you can get this link and show the image.

Let’s code!

IV – Creating our app

I’ll try to post a tutorial each two days. We are in the second tutorial, with basic examples, if you want to read all the tutorials, and at the end be able to develop any mobile app using Python, just follow my blog or subscribe you email to get updates. We are in the beginning posts, it’ll became more complex soon 🙂

Create a new folder and create a new file named Now, open your terminal and run the command bellow to create buildozer spec file.

$ buildozer init

Open your buildozer.spec and edit it. In this tutorial we are going to use Plyer, so make sure to have the following line:

requirements = kivy, plyer

You can check my version here:

title = Python Camera = camera
package.domain = com.wordpress.bytedebugger
source.dir = .
source.include_exts = py,png,jpg,kv,atlas
version = 1.0
requirements = kivy, plyer
fullscreen = 1
log_level = 2

V – Let’s code it

First, check the app version and import all necessary libs:

from import App #for the main app
from kivy.uix.floatlayout import FloatLayout #the UI layout
from kivy.uix.label import Label #a label to show information
from plyer import camera #object to read the camera

Now, create our basic UI:

class UI(FloatLayout):#the app ui
    def __init__(self, **kwargs):
        super(UI, self).__init__(**kwargs)
        self.lblCam = Label(text="Click to take a picture!") #create a label at the center
        self.add_widget(self.lblCam) #add the label at the screen

    def  on_touch_down(self, e):
        camera.take_picture('/storage/sdcard0/example.jpg', self.done) #Take a picture and save at this location. After will call done() callback

    def done(self, e): #receive e as the image location
        self.lblCam.text = e; #update the label to the image location

Create our app class:

class Camera(App): #our app
    def build(self):
        ui = UI()# create the UI
        return ui #show it

    def on_pause(self):
        #when the app open the camera, it will need to pause this script. So we need to enable the pause mode with this method
        return True

    def on_resume(self):
        #after close the camera, we need to resume our app.

Start it:

Camera().run() #start our app

App screens:

Screen #1

Screen #1

Screen #2

Screen #2

Screen #3

Screen #3

This picture was save at /storage/sdcard0/example.jpg.

You can get the full source here, and the apk here.

Unfortunately camera still under development, so we are not able to create complex apps with it. And it’s compatible with Android only.

If you want to, follow the blog and/or subscribe your email to get a notification about the next post.

Thank you for reading, if you need something, feel free to comment here.

Hey!! I’m available for freelances!! If you need anything, just contact me!

Contact me!!


11 thoughts on “Python for Android Tutorial #2 – Taking a picture

    • aron.bordin says:

      When I wrote the tutorial, I was trying to, but nothing worked with me.The camera class was under development, maybe now it’s working. I’ll be improving and continuing this tutorials soon, so if I found something, I’ll update the post


  1. sergey says:

    Your example works. But when I try this in my app it crashs after make foto. My programm uses sqlite3, may be it is cause. Did you try your code in more complex (difficult) app?


    • aron.bordin says:

      Yep, I coded a more complex app and I had no problem…
      Run it with buildozer android deploy run logcat, check the output if you got any error. It’s possible to debug the code using Android Studio, I’ll be posting it Friday or Saturday.
      And did you test your sqlite code without pictures?


      • Anonymous says:

        sql part of programm works properly. I have used logcat and ddms-tool from SDK to watch logs and to determinate how android-camera influence to programm.But python (kivy) part has no errors. Applet close immediateli after foto made. Like if “on_pause” and “on_resume” had no been defined ( Hope I express my mind clear ). I will try Android Studio. May be it say something interesting.


      • Anonymous says:

        I have debug programm by AndroidStudio and found the error. Android studio debugger more stable then sdk ddms-tool. Thanks a lot !


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s