我认为这是一件非常简单的事情,但它比整个应用程序的其余部分给我更多的工作。
我只想要一个按钮来打开我的应用程序的“已下载文件”文件夹。就这样。没有文件选择器,没有什么复杂的。该按钮所要做的就是打开一个包含默认应用程序的文件夹(如果没有默认应用程序,则打开应用程序选择器)。
我尝试了在 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/