Write to External Storage Android 12

564 views
Skip to first unread message

mackr...@gmail.com

unread,
Dec 14, 2022, 1:40:42 PM12/14/22
to Kivy users support
Hi,

What do I have to do to save a file to External Storage with Android 12?
I am trying this example

I have GPS working on Android 12 using Plyer from 

But now have trouble saving.  Setting permissions does not work.

Thank you,
Dave

Dave McCormick

unread,
Dec 14, 2022, 2:29:51 PM12/14/22
to Kivy users support
I think my answer might be in this thread.

Robert

unread,
Dec 14, 2022, 3:23:54 PM12/14/22
to Kivy users support

On modern devices all storage is external, shared storage is implemented as a database, private storage is implemented as a file system. (see link above)

You can copy to shared storage (the database) using the package referenced in that link.

As an aside if you run into issues with Kivy Camera on Android (there are several) use this camera https://github.com/Android-for-Python/Camera4Kivy  (and it handles both shared and private storage).

mackr...@gmail.com

unread,
Dec 15, 2022, 9:46:57 AM12/15/22
to Kivy users support
Thank you Robert!
For completeness, here is what I have that will save a picture to Download, Documents, Pictures, and probably a couple other places per your article.
Still need to go into the app settings and allow "Camera" and "Files and media".
Buildozer:
android.permissions = WRITE_EXTERNAL_STORAGE, CAMERA
android.api = 32

```
import kivy
from kivy.app import App
from kivy.uix.label import Label
from android.storage import app_storage_path,primary_external_storage_path,\
    secondary_external_storage_path
from android.permissions import request_permissions, check_permission, \
    Permission
from os.path import abspath
from os import getcwd
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time
from kivy.properties import StringProperty
from kivy.utils import platform

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()
    Label:
        text: root.PictureName
        size_hint_y: None
        height: '48dp'
''')
class CameraClick(BoxLayout):
   
    PictureName = StringProperty()
   
    def capture(self):
        '''
        Function to capture the images and give them the names
        according to their captured time and date.
        '''
       
        if platform == 'android':
            from android.storage import primary_external_storage_path
           
        camera = self.ids['camera']
        timestr = time.strftime("%Y%m%d_%H%M%S")
        picturename = join(primary_external_storage_path(),"Pictures/IMG_{}.png".format(timestr))
        # ~ picturename = join(primary_external_storage_path(),"Download/IMG_{}.png".format(timestr))
        # ~ picturename = join(primary_external_storage_path(),"Documents/IMG_{}.png".format(timestr))
        self.PictureName = picturename
        camera.export_to_png(picturename)
       
class TestCamera(App):

    def build(self):
        return CameraClick()

TestCamera().run()
 ```

mackr...@gmail.com

unread,
Dec 15, 2022, 9:48:52 AM12/15/22
to Kivy users support
How do you put code into code blocks on Google Groups?

Elliot Garbus

unread,
Dec 15, 2022, 10:09:00 AM12/15/22
to kivy-...@googlegroups.com

How do you put code into code blocks on Google Groups?

Right click and select “paste as plain text”.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/ce0de76f-c8f4-425e-b85e-08277ecd3f53n%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages