【问题标题】:Android open file and let user choose which program can open itAndroid打开文件并让用户选择哪个程序可以打开它
【发布时间】:2016-09-15 19:08:06
【问题描述】:

我正在开发一个需要在 ListView 中显示列表文件的程序。我得到了这个带有 path_of_files 的列表。但这就是我想问的:当我点击一个项目时,用户如何打开它?我想我想要一个弹出窗口让用户选择用户想要打开它的程序。 例如:我有一个路径:/sdcard/file.xls。当用户点击时,弹出窗口会显示一些应用程序,例如:Microsoft Excel、Google 电子表格(我只想使用 xls - 它只能使用它,仍然可以)。我该怎么做?

File sdCardRoot = Environment.getExternalStorageDirectory();
        File yourDir = new File(sdCardRoot, "directory");
        for (File f : yourDir.listFiles()) {
            if (f.isFile()) {
                String name = f.getName();
                Long lastModified = f.lastModified();
                if (name.toLowerCase().contains(".xls"))
                    fileObjList.add(new FileObj(0,name,lastModified));
            }
        }

这是我获取文件名和文件路径的代码。

【问题讨论】:

标签: android excel file


【解决方案1】:

首先,为您的 listView Items 设置一个侦听器。在处理程序回调中,您可以执行以下操作来打开 excel:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/vnd.ms-excel");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


try {
    startActivity(intent);
} 
catch (ActivityNotFoundException e) {
    Toast.makeText(context, "No Application Available to View Excel", Toast.LENGTH_SHORT).show();
}

【讨论】:

  • 它就像一个魅力。谢谢。但是,如果我想通过选择资源管理器应用程序打开文件夹路径,你能告诉我该怎么做吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
相关资源
最近更新 更多