community icon indicating copy to clipboard operation
community copied to clipboard

Update the camera example to include asking permissions for phone applications

Open catmasteryip opened this issue 4 years ago • 11 comments

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()

catmasteryip avatar Jul 15 '20 07:07 catmasteryip

This was a lifesaver honestly. Nothing else was working for me except explicitly asking for the android permissions.

SamSamhuns avatar Aug 18 '20 11:08 SamSamhuns

It is really much-needed update. But in my case it works only when I put request_permissions(...) call into build method.

FrCln avatar Sep 05 '20 16:09 FrCln

Thanks a lot, saved me hours of searching!!!!

pr1ntmaster avatar Oct 30 '20 12:10 pr1ntmaster

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?

matham avatar Oct 30 '20 16:10 matham

I think it is required to put the permissions call in the build stage, else there can be a fatal issue on_resume()

RobertFlatt avatar Nov 08 '20 17:11 RobertFlatt

@matham perhaps moving the permissions section inside a platform conditional would suffice?

pydsigner avatar Feb 02 '21 04:02 pydsigner

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).

matham avatar Feb 02 '21 18:02 matham

helloo, tks, it worked here! but when I deploy to mobile where will the photos be saved?

Osmar-Souza avatar Jan 19 '22 18:01 Osmar-Souza

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

RobertFlatt avatar Jan 19 '22 21:01 RobertFlatt

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)

cloud1980 avatar Mar 18 '22 11:03 cloud1980

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...

monojk avatar Aug 28 '22 12:08 monojk