Search code examples
androidpython-3.xcamerakivybuildozer

Kivy camera does not save pic on android phone


the code from this site is not working for me: https://kivy.org/doc/stable/examples/gen__camera__main__py.html It works perfectly on my pc. However, when I push it to my android phone with

buildozer -v android debug deploy run

The camera opens up normally and when I press 'Capture', it acts as if it takes a picture. However, when I look into the phone's gallery, I can't find any new picture. Is the picture saved somewhere that's not in the gallery? My app has a few more steps to upload that pic to google firebase. And it can never find the recently taken pic in the root directory. Again, it's working perfectly on PC. My buildozer spec is:

Permissionsandroid.permissions = INTERNET,CAMERA,WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE

I tried WRITE_INTERNAL_STORAGE and it didn't like it. So I guess writing to internal storage permission is given by default.

Another minor issue: the screen on the app is flipped sideways. It's showing the correct live pic but it's a bit weird. I tried switching between 'vertical' and 'horizontal' in the Builder.load_string but nothing changes.

Please assist. I appreciate any input.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time
Builder.load_string('''
<CameraClick>:
    orientation: 'vertical'
    Camera:
        id: camera
        resolution: (640, 480)
        play: False
    ToggleButton:
        text: 'Play'
        on_press: camera.play = not camera.play
        size_hint_y: None
        height: '48dp'
    Button:
        text: 'Capture'
        size_hint_y: None
        height: '48dp'
        on_press: root.capture()
''')


class CameraClick(BoxLayout):
    def capture(self):
        '''
        Function to capture the images and give them the names
        according to their captured time and date.
        '''
        camera = self.ids['camera']
        timestr = time.strftime("%Y%m%d_%H%M%S")
        camera.export_to_png("IMG_{}.png".format(timestr))
        print("Captured")


class TestCamera(App):

    def build(self):
        return CameraClick()


TestCamera().run()````

Solution

  • I have figured it out. Somehow the root directory doesn't work. To make it work, simply change

    camera.export_to_png("IMG_{}.png".format(timestr))
    

    to

    camera.export_to_png("/sdcard/IMG_{}.png".format(timestr))