File dialog in python sl4a

↘锁芯ラ 提交于 2019-12-23 01:52:50

问题


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()


来源:https://stackoverflow.com/questions/37795626/file-dialog-in-python-sl4a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!