Kivy Android Save Image

295 views
Skip to first unread message

purushottam yadav

unread,
Jan 27, 2021, 10:39:27 PM1/27/21
to Kivy users support

I have a simple program with the goal of saving an image to the Android Gallery.

Here is my code:


from plyer  import storagepath
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.image import Image as KVImage
from kivy.core.image import Image
from os import path


class SITApp(App):
    def build(self):
        return Button(text="save", on_press=self.save)

    def save(self, button):
        try:
            Image('download (1)0.jpg').save(str(storagepath.get_pictures_dir()) + 'download (1)0.jpg' )
        except Exception as e:
            button.text = str(e)


SITApp().run()
 
When I run the  code on  Ubuntu I am getting the error saying "unable to determine the format of the pixels " and Warning 
"
[WARNING] [MTD         ] Unable to open device "/dev/input/event3". Please ensure you have the appropriate permissions.
" .
what are the changes I should make to run this code on ubuntu and Android  ?

If anyone can help me save this picture in the Gallery of an Android device, it would be extremely appreciated !

planckp...@gmail.com

unread,
Jan 27, 2021, 11:53:58 PM1/27/21
to Kivy users support
1) Perhaps I don't understand the question, but wouldn't be simpler to copy the file download(1)0.jpg ?

from shutil import copyfile

2) As to the path, on Android this approach is good on build api=27, and maybe 28; but not 29 or 30.
The approach used by Plyer is depreciated on 29. See these two:

3) And for Android add WRITE_EXTERNAL_STORAGE in the buildozer.spec, and in the app with request_permissions()

I won't be around till next week, maybe somebody else can pick up any follow up?

purushottam yadav

unread,
Jan 28, 2021, 2:08:30 AM1/28/21
to Kivy users support
I made this changes considering  your  first suggestion  but I till get the warning 
"
[WARNING] [MTD         ] Unable to open device "/dev/input/event3". Please ensure you have the appropriate permissions.
" . 
and 

Unable to copy file. [Errno 21] Is a directory: '/home/purushottam2/Pictures'
 printed in terminal .





from plyer  import storagepath
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.image import Image as KVImage
from kivy.core.image import Image
from os import path

import os
import shutil
from shutil import copyfile
from sys import exit

class SITApp(App):

    source = str(storagepath.get_pictures_dir())
    target = 'photo-1503023345310-bd7c1de61c7d.jpeg'


    def build(self):
        return Button(text="save", on_press=self.save)

    def save(self, button):


        #assert not os.path.isabs(source)

        self.target = os.path.join(self.target, os.path.dirname(self.source))

        # create the folders if not already exists
        #os.makedirs(target)

        # adding exception handling
        try:
            copyfile(self.source, self.target)
        except IOError as e:
            print("Unable to copy file. %s" % e)
        except:
            print("Unexpected error:", sys.exc_info())

#        try:
#            Image('download (1)0.jpg').save(   str(storagepath.get_pictures_dir()) + 'download (1)0.jpg' )
#        except Exception as e:
#            button.text = str(e)


SITApp().run()
Reply all
Reply to author
Forward
0 new messages