android - 打开卡上的文件夹

标签 android android-intent android-sdcard

我认为这是一件非常简单的事情,但它比整个应用程序的其余部分给我更多的工作。

我只想要一个按钮来打开我的应用程序的“已下载文件”文件夹。就这样。没有文件选择器,没有什么复杂的。该按钮所要做的就是打开一个包含默认应用程序的文件夹(如果没有默认应用程序,则打开应用程序选择器)。

我尝试了在 StackOverflow 上看到的不同解决方案,但似乎没有任何效果。

示例 1:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/MyDownloadedFiles/");
intent.setDataAndType(uri, "text/plain");
startActivity(Intent.createChooser(intent, getString(R.string.action_downloaded)));

在我的 Android 4.4 上,这会打开 KitKat 文件选择器,甚至不会在我想要的文件夹中打开...它似乎默认为卡根。但我确定该文件夹存在并且提供给 Intent 的路径确实是正确的。

示例 2:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/MyDownloadedFiles/");
intent.setDataAndType(uri, "text/plain");
startActivity(Intent.createChooser(intent, getString(R.string.action_downloaded)));

这个打开一些空白 Activity ,弹出窗口说:“获取文件内容时发生错误:MyDownloadedFiles”。

我怎样才能让我的应用程序拥有一个快捷方式,指向将内容放入其中的文件夹?

最佳答案

哇..终于!!在我尝试了很多事情之后,唯一可行的方法是先将 URI 字符串包装到一个文件中,然后再执行一个 Uri.fromFile:

File file = new File(Environment.getExternalStorageDirectory().getPath() + "/MyDownloadedFiles/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "*/*");
startActivity(Intent.createChooser(intent, getString(R.string.action_downloaded)));

关于android - 打开卡上的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24855837/

相关文章:

android - Environment.getExternalStorageDirectory().getAbsolutePath() 返回 sdcard0 但我的 SD 在 sdcard1 中

android - window.open 链接不会在 Windows Phone 8 的 cordova 应用程序中打开

android - FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 未在 Nexus 10 (Android 4.4.2) 中设置

java - Fragment Activity 中的 Android 电池统计信息

java - 如何使用 Intent.FLAG_ACTIVITY_CLEAR_TOP 清除 Activity Stack?

android - 为什么 queryIntentActivities() 方法中指定的标志设置为零?

android - IabHelper 类不起作用?

android - 我们如何以编程方式检查 android 手机是否支持外部 SD 卡?

android - 列出 SD 卡中的所有图像

android - 从 singleInstance Activity 启动的新 Activity 的不同行为