Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 打开卡上的文件夹_Android_Android Intent_Android Sdcard - Fatal编程技术网

Android 打开卡上的文件夹

Android 打开卡上的文件夹,android,android-intent,android-sdcard,Android,Android Intent,Android Sdcard,我原以为这很简单,但它比整个应用程序的其他部分给了我更多的工作 我只需要一个按钮来打开我的应用程序的“下载文件”文件夹。这就是全部。没有文件选择器,没有什么复杂的。按钮所要做的就是打开一个带有默认应用程序的文件夹(如果没有默认应用程序,则打开应用程序选择器) 我尝试了在StackOverflow上看到的不同的解决方案,但似乎没有任何效果 例1: Intent intent = new Intent(Intent.ACTION_GET_CONTENT); Uri uri = Uri.parse(E

我原以为这很简单,但它比整个应用程序的其他部分给了我更多的工作

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

我尝试了在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文件选择器,甚至不会在我想要的文件夹中打开。。。它似乎默认为卡根。但我确信该文件夹存在,并且提供给意图的路径确实是正确的

例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)));
这一个打开一些空白的活动,弹出窗口说:“获取文件内容时出错:MyDownloadedFiles”


我如何使我的应用程序只需有一个文件夹的快捷方式就可以将内容放入其中?

我想这一个对你有用:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
    + "/yourFolder/");
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "Open /sdcard/yourFolder"));

哇。。最后经过多次尝试,唯一有效的方法是先将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)));

你好我已经试过了,它与我的示例1非常相似。唯一的区别是类型。在使用text/plain之前,我尝试过使用text/plain。那么,转换(听起来很奇怪)和
“*/*”
的组合是关键?不是单独的“*/*”?或者可能是单独的转换?我认为这里的关键是File对象可能会在前面加上“File://”或其他任何东西,这使得它能够工作。“/”不是确定的解决方案,因为如果我在类型中使用“Uri.parse()”和“/”的话,它将无法工作-将与我上面的示例1一样。我很奇怪,你保留了“”不写“星号(*)……无论如何,如果kitkat要求这样做,如果您记住
Environment.getExternalStorageDirectory()
已经为您提供了一个文件对象,那么您可以稍微简化代码(最好保持简单,这样我们就不会产生从文件到字符串再到文件再到Uri的神话:P).恭喜!哦,星号没有出现是因为它们变成了粗体。很抱歉。没问题。不管怎样,这个问题一直是一个很好的提醒,提醒我们在每个Android版本中保持警惕:)