File dialog in python sl4a

2019-06-03 02:14发布

问题:

I am looking for an simple file chooser dialog in sl4a. I have found a few native dialogs here but did not find the one I was looking for.

I wish I could save time by finding something readily available. A minimal code like filename = fileopendialog() would be a bonus.

Any ideas ?

回答1:

I decided to write my own (see below for reference). This could probably be made better, any suggestions welcome.

import android, os, time

droid = android.Android()
# Specify root directory and make sure it exists.
base_dir = '/sdcard/sl4a/scripts/'
if not os.path.exists(base_dir): os.makedirs(base_dir)

def show_dir(path=base_dir):
    """Shows the contents of a directory in a list view."""
    #The files and directories under "path"
    nodes = os.listdir(path)
    #Make a way to go up a level.
    if path != base_dir: 
        nodes.insert(0, '..')
    droid.dialogCreateAlert(os.path.basename(path).title())
    droid.dialogSetItems(nodes)
    droid.dialogShow()
    #Get the selected file or directory .
    result = droid.dialogGetResponse().result
    droid.dialogDismiss()
    if 'item' not in result:
       return
    target = nodes[result['item']]
    target_path = os.path.join(path, target)
    if target == '..': target_path = os.path.dirname(path)
    #If a directory, show its contents .
    if os.path.isdir(target_path): 
        show_dir(target_path)
    #If an file display it.
    else:
        droid.dialogCreateAlert('Selected File','{}'.format(target_path))
        droid.dialogSetPositiveButtonText('Ok')
        droid.dialogShow()
        droid.dialogGetResponse()

if __name__ == '__main__':
    show_dir()