示例#1
0
class ShareSendExample(App):
    def build(self):
        self.create_test_file()
        self.share = ShareSend()
        self.picker = Picker(self.picker_callback)
        request_permissions([Permission.READ_EXTERNAL_STORAGE])

        b1 = Button(text='Share plain TEXT via a ShareSheet',
                    on_press=self.button1_pressed)
        b2 = Button(text='Share "test.html" FILE via a ShareSheet',
                    on_press=self.button2_pressed)
        b3 = Button(text='Pick a file to share with Gmail',
                    on_press=self.button3_pressed)
        b4 = Button(
            text=
            'Pick a video/mp4 to share with Receive\nThe Recieve example must be installed.',
            on_press=self.button4_pressed)
        box = BoxLayout(orientation='vertical')
        box.add_widget(b1)
        box.add_widget(b2)
        box.add_widget(b3)
        box.add_widget(b4)
        self.box = box
        return box

    def button1_pressed(self, b):
        self.share.share_text('Greetings Earthlings')

    def button2_pressed(self, b):
        uri = SharedStorage().getUri('test.html')
        if uri:
            self.share.share_uri(uri)

    def button3_pressed(self, b):
        self.target = 'com.google.android.gm'
        self.picker.pick_file()

    def button4_pressed(self, b):
        self.target = 'org.test.receive'
        self.picker.pick_file('video/mp4')

    def picker_callback(self, uri):
        if uri:
            self.share.share_uri(uri, self.target)

    def create_test_file(self):
        # create a file in Private storage
        filename = join(PrivateStorage().getCacheDir(), 'test.html')
        with open(filename, "w") as f:
            f.write("<html>\n")
            f.write(" <head>\n")
            f.write(" </head>\n")
            f.write(" <body>\n")
            f.write("  <h1>Greetings Earthlings<h1>\n")
            f.write("  <h1>We come in please?<h1>\n")
            f.write(" </body>\n")
            f.write("</html>\n")
        # Insert the test case in this app's Shared Storage so it will have a Uri
        SharedStorage().insert(filename)
示例#2
0
class StorageExample(App):

    def build(self):
        # create picker listener
        self.picker = Picker(self.picker_callback)
        # Permission required for Picker()
        request_permissions([Permission.READ_EXTERNAL_STORAGE])
        # cleanup
        temp = PrivateStorage().getCacheDir()
        if not temp:
            temp = PrivateStorage().getCacheDir('internal')
        temp = join(temp,"MediaStore")
        if exists(temp):
            rmtree(temp)

        # layout
        self.label_lines = []
        self.label = Label(text = 'Greetings Earthlings')
        self.button = Button(text = 'Use Picker to get a Document',
                             on_press = self.picker_start,
                             size_hint=(1, .15))
        self.layout = BoxLayout(orientation='vertical')
        self.layout.add_widget(self.label)
        self.layout.add_widget(self.button)
        return self.layout

    def on_start(self):
        # Private Storage operations
        self.append("PRIVATE STORAGE")
        self.append("Install Dir:  " + str(exists(getcwd())))
        self.append("FilesDir:  " + str(exists(PrivateStorage().getFilesDir())))
        self.append("CacheDir:  " + str(exists(PrivateStorage().getCacheDir())))

        # Shared Storage operations
        self.append("PUBLIC STORAGE:")
        res0 = SharedStorage().insert('./test.txt', 'Documents')
        res1 = SharedStorage().insert('./test.txt', sub_dir= 'a/b')
        res2 = SharedStorage().insert('./test.txt', 'Downloads')
        res3 = SharedStorage().insert('./test.jpg', 'Pictures')
        res4 = SharedStorage().insert('./test.mp3')
        res5 = SharedStorage().insert('./test.ogg', 'Music')
        copy_path1 = SharedStorage().retrieve('test.txt')
        copy_path2 = SharedStorage().retrieve('test.txt', sub_dir = 'a/b')
        res6 = SharedStorage().delete('test.mp3', 'Music')
        
        self.append("insert test.txt  " + str(res0))
        self.append("insert a/b/test.txt:  " + str(res1))
        self.append("insert test.txt in Downloads:  " + str(res2))
        self.append("insert test.jpg:  " + str(res3))
        self.append("insert test.mp3:  " + str(res4))
        self.append("insert test.ogg:  " + str(res5))
        self.append("retrieve test.txt  " + str(exists(copy_path1)))
        self.append("retrieve a/b/test.txt  " + str(exists(copy_path2)))
        self.append("deleted test.mp3  " + str(res6))
        self.display()

    # Picker interface
    def picker_start(self,bt):
        self.picker.pick_file("application/*")

    def picker_callback(self,uri):
        try:
            path = SharedStorage().retrieveUri(uri)
            self.append("Retrieved from Picker  " + str(exists(path)))
            self.display()
        except Exception as e:
            print('ERROR StorageExample.picker_callback():\n' + str(e))

    # Label text
    def append(self, name):
        self.label_lines.append(name)

    @run_on_ui_thread
    def display(self):
        if self.label:
            self.label.text = ''
            for r in self.label_lines:
                self.label.text += fill(r, 40) + '\n'