【问题标题】:Need file save dialog for android需要安卓的文件保存对话框
【发布时间】:2013-02-19 17:51:58
【问题描述】:

我需要 android 的文件保存对话框,以保存我的应用生成的 png 文件。

我试过Intent intentFileSave = new Intent(Intent.ACTION_PICK);

但这没有用。 如果我没有任何文件管理器,Android 是否有默认的“另存为”对话框??

我还尝试在我的应用程序中集成 OI 文件管理器。 但我不知道,OI 文件管理器的许可证是否允许将其用作库。

是否有任何其他选项要求用户选择文件的位置和名称??

【问题讨论】:

标签: android android-intent dialog


【解决方案1】:

android 是否有默认的“另存为”对话框??

不,主要是因为应用程序要么将文件保存在其应用程序的内部或外部存储部分中,要么将它们保存在以Environment 类命名的特定公共目录中。然后他们使用ACTION_SEND 与其他可以使用它们的服务共享文件。

但我不知道,OI 文件管理器的许可证是否允许将其用作库。

我在他们的 GitHub 存储库中没有看到任何许可证的迹象,这很不幸。

还有其他选项可以让用户选择文件的位置和名称吗?

除了完全不这样做(并保存到标准位置)之外,您还可以:

  • 通过特定的Intent 操作(例如,org.openintents.action.PICK_DIRECTORY)链接到 OI 文件管理器等文件管理器应用,如果没有,则提示用户安装一个

  • 寻找其他一些库来选择目录和/或文件

  • 创建自己的

【讨论】:

  • 如果您可以通过将 Intent 传递给 OI 文件管理器作为文件选择对话框,我认为许可证并不重要,因为您没有以任何方式链接到它?
  • @Torp:是的。我推测 OP 想要实际集成源代码(“允许将其用作库”)。
  • @CommonsWare 是的,我想将源代码作为库与我的项目集成。但我不应该这样做,对吧?您告诉我通过 Intent 操作启动它,但如果未安装 OI 文件管理器,则它不会提示我将其安装在我的模拟器中。有什么办法可以让 OI 文件管理器作为我的应用程序的先决条件??
  • @themilan:“有没有办法让 OI 文件管理器作为我的应用程序的先决条件”——不幸的是,没有。您可以使用PackageManager 来确定它(或您支持的任何东西)是否已安装,然后提示用户从 Play 商店下载一个。您甚至可以使用 market: Uri 将它们发送到 Play 商店应用中的正确屏幕。
【解决方案2】:

我也试过了,

intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(startDir, "vnd.android.cursor.dir/*");

但它向我展示了所有可能的意图,例如文件管理器、图库、音乐、内容等...
我没有找到任何方式来决定支持“另存为”功能的可能意图。

所以,我有以下解决方案。 在这里,我只需要为支持另存为功能的不同文件浏览器添加越来越多的尝试。

int iLoop = 0;
int iTryCount = 2;
for (iLoop = 0; iLoop < iTryCount; iLoop++)
{
    Intent intent = null;

    if (iLoop == 0)
    {
        // OI File Manager
        intent = new Intent(Intent.ACTION_PICK);
        intent.setData(startDir);
        intent.setAction("org.openintents.action.PICK_FILE");
        intent.putExtra("org.openintents.extra.TITLE", "Save As...");
    }
    else if (iLoop == 1)
    {
        // AndExplorer
        intent = new Intent(Intent.ACTION_PICK);
        intent.setDataAndType(startDir,
                "vnd.android.cursor.dir/lysesoft.andexplorer.file");
        intent.putExtra("explorer_title", "Save As...");
        intent.putExtra("browser_filter_extension_whitelist", "*.png");
        intent.putExtra("browser_line", "enabled");
        intent.putExtra("browser_line_textfield", "file1.png");
    }

    if (intent != null)
    {
        try
        {
            // Try to launch activity
            startActivityForResult(intent, REQUEST_SAVE_FILE);

            // TODO: Remove this notification on Publish
            Toast.makeText(m_baseSurfaceView.getContext(),
                    "Try : " + iLoop, Toast.LENGTH_SHORT).show();

            // If everything gone well, then break from here
            // otherwise see catch(..) block
            break;
        }
        catch (Exception e)
        {
            e.printStackTrace();

            // If all tries are done, then conclusion is that,
            // the user needs to install some file-manager application
            // to locate where to save the file
            if (iLoop == iTryCount - 1)
            {
                Toast.makeText(
                        m_baseSurfaceView.getContext(),
                        "Please Install some File Manager"
                                + " application to locate destination.",
                        Toast.LENGTH_LONG).show();
            }
        }
    }
}

如果有任何建议请评论。

【讨论】:

    【解决方案3】:

    OI 文件管理器不需要许可证即可使用,因为它是一个独立的软件。将 Open Intents 项目视为某种 API。据我所知,API 不需要许可证。

    至于保存文件,不幸的是,OI 文件管理器没有为您提供“另存为”选项,但您可以轻松解决这个问题!只需让用户在您的应用中输入他们想要的文件名,然后使用PICK_DIRECTORY intent 让他们告诉您将文件放在哪里!

    【讨论】:

      猜你喜欢
      • 2020-11-03
      • 2012-01-10
      • 1970-01-01
      • 2021-12-18
      • 2011-08-23
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多