community
community copied to clipboard
Update the camera example to include asking permissions for phone applications
Is your feature request related to a problem? Please describe. I was frustrated about the camera example that it throws the JVM exception error at opening android camera. Now I figure out it just needs android permissions to run on android phone.
Describe the solution you'd like The code can run on android phones but not on PC. To run it on PC you have to remove android.permissions and other associated methods.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.utils import platform
from android.permissions import request_permissions, Permission
import time
request_permissions([
Permission.CAMERA,
Permission.WRITE_EXTERNAL_STORAGE,
Permission.READ_EXTERNAL_STORAGE
])
Builder.load_string('''
<CameraClick>:
orientation: 'vertical'
Camera:
index: 0
id: camera
resolution: (640,480)
play: True
allow_stretch: True
canvas.before:
PushMatrix
Rotate:
angle: -90
origin: self.center
canvas.after:
PopMatrix
Button:
text: 'Capture'
size_hint_y: None
height: '48dp'
on_press: root.capture()
''')
class CameraClick(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
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()
This was a lifesaver honestly. Nothing else was working for me except explicitly asking for the android permissions.
It is really much-needed update. But in my case it works only when I put request_permissions(...)
call into build
method.
Thanks a lot, saved me hours of searching!!!!
A PR adding this to the example would be welcome (commented out so people can remove the comment guards when testing for android, with a text comment indicating this).
Although I don't you normally ask these permissions during the build stage? Or do you still need to request it before using?
I think it is required to put the permissions call in the build stage, else there can be a fatal issue on_resume()
@matham perhaps moving the permissions section inside a platform conditional would suffice?
I don't think it's a good idea to mix deployment stuff (like permissions) into the core framework because they are separate stages/processes. And we haven't done it for other permissions either.
Documenting where the permission needs to be asked is the better approach (especially considering what @RobertFlatt said).
helloo, tks, it worked here! but when I deploy to mobile where will the photos be saved?
https://github.com/kivy/python-for-android/blob/develop/doc/source/apis.rst#storage-paths
But files can only be saved to app_local_storage() on Android >= 10 . Your app is then responsible for moving to the MediaStore if required.
Or try https://github.com/Android-for-Python/Camera4Kivy
We can use the same code both in PC and Android Pad
try: from android.permissions import request_permissions, Permission request_permissions([ Permission.CAMERA, Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE, Permission.INTERNET, Permission.BODY_SENSORS, Permission.BLUETOOTH ]) except Exception as e: logging.warn(e)
Will the code be updated finally in https://kivy.org/doc/stable/examples/gen__camera__main__py.html please? It took me many hours to debug with non working tips, an finally to found this solution...