Skip to content

Three issues on Android #29

Open
Open
@RobertFlatt

Description

  1. App crashes if index=1 , I was expecting the selfie camera; index = 0 works as expected.
  2. How to release and attach the camera during on_pause() and on_resume() events ?
  3. on_camera_ready() is not called because on_index() is not called by default.

Despite the issues, the code is a joy to read. :)

Example code (not as pretty as I would like because no on_camera_ready) , and log for the first issue follow:

#!/usr/bin/env python
from kivy.app import App
from kivy.lang import Builder
from kivy.utils import platform
from kivy.clock import Clock
from os.path import isdir
from os import mkdir
try:
    from android.permissions import request_permissions, check_permission, \
        Permission
    from android.storage import primary_external_storage_path
except:
    pass

kv = """
#:import XCamera kivy_garden.xcamera.XCamera
FloatLayout:
    orientation: 'vertical'
    XCamera:
        id: xcamera
        on_picture_taken: app.picture_taken(*args)
        on_camera_ready: app.camera_ready()
    BoxLayout:
        orientation: 'horizontal'
        size_hint: 1, None
        height: sp(50)
        Button:
            text: 'Set landscape'
            on_release: xcamera.force_landscape()
        Button:
            text: 'Restore orientation'
            on_release: xcamera.restore_orientation()
"""

class CameraApp(App):
    def build(self):
        return Builder.load_string(kv)

    def on_start(self):
        if platform == 'android':
            # ISSUE 1 :
            # Crash when index =1 , index = 0 is OK
            self.root.ids.xcamera.index = 0
            request_permissions([Permission.WRITE_EXTERNAL_STORAGE,
                                 Permission.CAMERA],
                                self.setup_storage)
            self.setup_storage([],[])

    # ISSUE 2 : how to open and close camera ?
    def on_pause(self):
        return True

    def on_resume(self):
        pass

    def camera_ready(self):
        # In kv above     on_camera_ready: app.camera_ready()
        # ISSUE 3 :
        # This is never called because on_index is not called by default
        pass
        
    def setup_storage(self,permissions,grants):
        if platform == 'android':
            if check_permission(Permission.WRITE_EXTERNAL_STORAGE):
                path = join(primary_external_storage_path(),'Pictures')
                if not isdir(path):
                    mkdir(path)
                path = join(path,'Xcamera')
                if not isdir(path):
                    mkdir(path)
                self.root.ids.xcamera.directory = path
            if check_permission(Permission.CAMERA):
                # One sec hack because on_camera_ready() isnt called
                Clock.schedule_once(self.force_landscape,1)

    def force_landscape(self,dt):
        # Force long edge of image to be parallel to long edge of screen.
        # This is independent of the orientation of the screen in the world.
        # At least that is what happens on my Pixel 3
        self.root.ids.xcamera.force_landscape()

    def picture_taken(self, obj, filename):
        print('Picture taken and saved to {}'.format(filename))

def main():
    CameraApp().run()


if __name__ == '__main__':
    main()

Crash log

07-27 13:43:50.324 31415 31444 I python  : [INFO   ] [Base        ] Leaving application in progress...
07-27 13:43:50.324 31415 31444 I python  :  Traceback (most recent call last):
07-27 13:43:50.324 31415 31444 I python  :    File "/home/bobf/ex/xcam/.buildozer/android/app/main.py", line 91, in <module>
07-27 13:43:50.325 31415 31444 I python  :    File "/home/bobf/ex/xcam/.buildozer/android/app/main.py", line 87, in main
07-27 13:43:50.325 31415 31444 I python  :    File "/home/bobf/ex/xcam/.buildozer/android/platform/build-arm64-v8a/build/python-installs/xcam/kivy/app.py", line 855, in run
07-27 13:43:50.325 31415 31444 I python  :    File "/home/bobf/ex/xcam/.buildozer/android/platform/build-arm64-v8a/build/python-installs/xcam/kivy/base.py", line 504, in runTouchApp
07-27 13:43:50.325 26747 26747 W ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@79e1678
07-27 13:43:50.325 31415 31444 I python  :    File "/home/bobf/ex/xcam/.buildozer/android/platform/build-arm64-v8a/build/python-installs/xcam/kivy/core/window/window_sdl2.py", line 747, in mainloop
07-27 13:43:50.326 31415 31444 I python  :    File "/home/bobf/ex/xcam/.buildozer/android/platform/build-arm64-v8a/build/python-installs/xcam/kivy/core/window/window_sdl2.py", line 479, in _mainloop
07-27 13:43:50.326 31415 31444 I python  :    File "/home/bobf/ex/xcam/.buildozer/android/platform/build-arm64-v8a/build/python-installs/xcam/kivy/base.py", line 339, in idle
07-27 13:43:50.326 31415 31444 I python  :    File "/home/bobf/ex/xcam/.buildozer/android/platform/build-arm64-v8a/build/python-installs/xcam/kivy/clock.py", line 591, in tick
07-27 13:43:50.326 31415 31444 I python  :    File "kivy/_clock.pyx", line 384, in kivy._clock.CyClockBase._process_events
07-27 13:43:50.326 31415 31444 I python  :    File "kivy/_clock.pyx", line 414, in kivy._clock.CyClockBase._process_events
07-27 13:43:50.326 31415 31444 I python  :    File "kivy/_clock.pyx", line 412, in kivy._clock.CyClockBase._process_events
07-27 13:43:50.327 31415 31444 I python  :    File "kivy/_clock.pyx", line 167, in kivy._clock.ClockEvent.tick
07-27 13:43:50.327 31415 31444 I python  :    File "/home/bobf/ex/xcam/.buildozer/android/platform/build-arm64-v8a/build/python-installs/xcam/kivy/core/camera/camera_android.py", line 155, in _update
07-27 13:43:50.327 31415 31444 I python  :    File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
07-27 13:43:50.327 31415 31444 I python  :    File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
07-27 13:43:50.328 31415 31444 I python  :    File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
07-27 13:43:50.328 31415 31415 V SDL     : onWindowFocusChanged(): false
07-27 13:43:50.328 31415 31444 I python  :    File "/home/bobf/ex/xcam/.buildozer/android/platform/build-arm64-v8a/build/python-installs/xcam/kivy/uix/camera.py", line 111, in _camera_loaded
07-27 13:43:50.328 31415 31444 I python  :  AttributeError: 'NoneType' object has no attribute 'size'
07-27 13:43:50.328 31415 31444 I python  : Python for android ended.




Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions