Kivy--Export Window as png on Android

2019-09-11 05:52发布

问题:

I am trying to create a app on android with wich you fill in a few TextInputs and then give your signature on a marked space left for that, I have 2 buttons, one for clearing and one for exporting the window as a png, however when I buildozer this as a .apk and then run it on my Tablet everything still looks fine, and pressing the exporting button dont give a error, but I cant find a image for the life of me, someone said to look in the directory the code is in, but in my case its a app (.apk), I'll add my code, even though i dont think it has much to do with the problem.

Thanks for the help in advance

__version__ = "0.2"
from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Ellipse, Line
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.config import Config
from kivy.base import EventLoop


class MyPaintWidget(Widget):
    Window.clearcolor = (0.95, 0.95, 0.85, 1)

    def on_touch_down(self, touch):
        color = (0,0,0)
        with self.canvas:
            Color(*color, mode='hsv')
            d = 3
            Ellipse(pos=(touch.x - d / 2, (touch.y - d / 2)+50), size=(d, d))
            touch.ud['line'] = Line(points=(touch.x, touch.y+50),width=1.5)

    def on_touch_move(self, touch):
        touch.ud['line'].points += [touch.x, touch.y+50]


class WaentjiesApp(App):
    display_width = 1200
    global parent
    def build(self):
        global parent
        Window.size = (self.display_width, 500)
        parent = FloatLayout()
        self.painter = MyPaintWidget()
        save_button = Button(text = 'Save',pos=(0,0),size_hint=(.20,.20),on_release=self.save_canvas)
        clear_button = Button(text = 'Clear',pos=(300,0),size_hint=(.20,.20),on_release=self.clear_canvas)
        name_text = TextInput(text='',pos=(0,370),size_hint=(.15,.15))
        van_text = TextInput(text='',pos=(200,370),size_hint=(.15,.15))
        adres_text = TextInput(text='',pos=(0,220),size_hint=(.15,.15))
        waentjie_text = TextInput(text='',pos=(200,220),size_hint=(.15,.15))
        name_label = Label(text='Naam',pos=(0,450),size_hint=(.15,.15),color=(0,0,0,1))
        van_label = Label(text='Van',pos=(200,450),size_hint=(.15,.15),color=(0,0,0,1))
        adres_label = Label(text='Adres',pos=(0,300),size_hint=(.15,.15),color=(0,0,0,1))
        waentjie_label = Label(text='Waentjie',pos=(200,300),size_hint=(.15,.15),color=(0,0,0,1))
        handtekening1_label = Label(text='Handtekening',pos=(400,350),size_hint=(.15,.15),color=(0,0,0,1))
        handtekening2_label = Label(text='......................................................................................................................................',pos=(600,190),size_hint=(.15,.15),color=(0,0,0,0.5))
        agtergrond = Label()
        parent.add_widget(handtekening2_label)
        parent.add_widget(self.painter)
        parent.add_widget(save_button)
        parent.add_widget(clear_button)
        parent.add_widget(name_text)
        parent.add_widget(van_text)
        parent.add_widget(adres_text)
        parent.add_widget(name_label)
        parent.add_widget(van_label)
        parent.add_widget(adres_label)
        parent.add_widget(waentjie_text)
        parent.add_widget(waentjie_label)
        parent.add_widget(handtekening1_label)
        return parent

    def save_canvas(self, obj):
        global parent
        parent.export_to_png('b.png')

    def clear_canvas(self, obj):
        self.painter.canvas.clear()


WaentjiesApp().run()

some of the variables and names might seem weird, but english is not my first language so I tend to choose other-langage names

回答1:

The file is saved in the current directory, which you can access with e.g. os.path.realpath('.') from your script. This directory is not accessible to other applications. If you want to access the file from other applications, save it somewhere in the external storage dir (with Kivy, you can use App.user_data_dir).