首页 > 解决方案 > 如何在 Android 应用程序中使用外部应用程序打开 SGF 文件?

问题描述

我正在尝试在外部应用程序中打开一个 SGF 文件(一种用于存储围棋游戏的流行格式)。我当前的代码设法启动了一个应用程序,然后默认情况下无法加载它。我似乎无法改变这种行为。

使用其他应用程序打开文件我得到不一致的行为。文件浏览器说没有应用程序能够打开 sgf 文件并将我重定向到 Play 商店(无论我随后安装了哪些应用程序)。但是,互联网浏览器给了我一个弹出窗口,告诉我选择 2 个合适的应用程序之一。我希望在我的应用程序中具有相同的行为,用户可以在其中明确选择应用程序来打开它。

我当前的代码(似乎确实打开了一个应用程序,显示一条消息“加载失败”并退出,没有遇到异常):

file = self.files[self.file_ix]
if platform == "android":
    from jnius import autoclass

    Intent = autoclass("android.content.Intent")
    Uri = autoclass('android.net.Uri')
    PythonActivity = autoclass("org.renpy.android.PythonActivity")
    try:
        intent = Intent()
        intent.setAction(Intent.ACTION_VIEW)
        intent.setDataAndType(Uri.parse(file), "application/x-go-sgf")
        PythonActivity.mActivity.startActivity(intent)
    except Exception as e:
        self.hint.text = f"No SGF Viewer found: {e}"
else:
    os.system(f"xdg-open '{file}'")

如何更改它以获取带有应用程序选择的弹出窗口?

标签: pythonandroidkivy

解决方案


推荐阅读