Skip to content
Advertisement

Kivi: How to set Path for storing Output.txt file of Android app

I am making android app with kivy and python, I have function what will create record in TXT format, in my Project it is in same folder as the .PY file, but i know once i create .apk then it can be problem where the file will be stored. For example I would like to store that Tracking.txt file in Download folder of Android device. How i can define this please?

This is how it is defined now, it should safe/open file where is the app.

import kivy
from datetime import datetime
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.image import Image
from kivy.core.audio import SoundLoader
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.utils import platform
import os

if platform == 'android':
    import android
    from android.permissions import request_permissions, Permission
    request_permissions([Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE])
    from android.storage import primary_external_storage_path
    dir = primary_external_storage_path()
    download_dir_path = os.path.join(dir, 'Download')

Window.size = (480, 800)

class MyGrid(GridLayout):
    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)

        self.cols = 1  # Set columns for main layout
        self.inside = GridLayout(cols=2, row_force_default=True,
                                 row_default_height=50, padding=20,
                                 spacing=10)  # Create a new grid layout
        self.add_widget(Label(text="Tracking Number n Checker", halign="center", font_size=40))  # Top Label
        self.inside.add_widget(Label(text="Work Number:", halign="center", font_size=20))  # Add a label widget
        self.worknumb = TextInput(text_validate_unfocus=True, multiline=False, font_size=20, halign="center")
        self.inside.add_widget(self.worknumb)
        self.inside.add_widget(Label(text="Tracking nNO. Scan:", halign="center", font_size=20))
        self.tracknumb = TextInput(multiline=False, font_size=15, halign="center")  # Create a Text input box stored in the name variable
        self.inside.add_widget(self.tracknumb)  # Add the text input widget to the GUI

        self.add_widget(self.inside)  # Add the interior layout to the main
        self.submit = Button(text="Submit", font_size=40, size_hint =(.5, .5))  # Submit button
        self.add_widget(self.submit)
        self.submit.bind(on_press=self.send_tracknumb)
        self.resultbox = Image(source="status.png")  #image box on bottom
        self.add_widget(self.resultbox)

        Window.bind(on_key_down=self.pressed)
        Clock.schedule_once(self.focus_worknumb, 1)
    def pressed(self, instance, keyboard, keycode, text, modifiers):
        if keycode == 40 or keycode == 13 or keycode == 8:
            self.send_tracknumb(None)

    def focus_worknumb(self, _):
        self.worknumb.focus = True
    def focus_tracknumb(self, _):
        self.tracknumb.focus = True
    def send_tracknumb(self, _):
        tracknumb = self.tracknumb.text
        worknumb = self.worknumb.text
        errorsound = SoundLoader.load("incorrect.mp3")  # add sound to the scanning
        correctsound = SoundLoader.load("correct.ogg")
        self.tracknumb.text = ""  # Reset text to blank in each text input
        Clock.schedule_once(self.focus_tracknumb, 0.2)
        f = open(os.path.join(download_dir_path, 'tracking.txt'), "a")
        tr = tracknumb
        wr = worknumb
        time = str(datetime.now())

Edit: I used the function suggested in post below, but now it showing that it cannot find the Android, what i somehow installed from the suggestion what pop up in Pycharm, but the storage and premmisions are not found. What am i missing in Import part?

Advertisement

Answer

You can use the below code snippet to get hold of the Download directory path in android. And then use the path as appropriate.

from kivy.utils import platform
import os

if platform == 'android':
    from android.storage import primary_external_storage_path
    dir = primary_external_storage_path()
    download_dir_path = os.path.join(dir, 'Download')
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement