Android:如何通过 Intent 打开特定文件夹并在文件浏览器中显示其内容? [英] Android: How to open a specific folder via Intent and show its content in a file browser?

查看:17
本文介绍了Android:如何通过 Intent 打开特定文件夹并在文件浏览器中显示其内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这很容易,但不幸的是事实并非如此.

I thought this would be easy but as it turns out unfortunately it's not.

我有什么:

我的外部存储设备上有一个名为myFolder"的文件夹(不是 sd 卡,因为它是 Nexus 4,但这应该不是问题).该文件夹包含一些 *.csv 文件.

I have a folder called "myFolder" on my external storage (not sd card because it's a Nexus 4, but that should not be the problem). The folder contains some *.csv files.

我想要的:

我想编写一种执行以下操作的方法:显示各种应用程序(文件浏览器),我可以从中选择一个(见图).单击它后,所选文件浏览器应启动并向我显示myFolder"的内容.不多不少.

I want to write a method which does the following: Show a variety of apps (file browsers) from which I can pick one (see picture). After I click on it, the selected file browser should start and show me the content of "myFolder". No more no less.

我的问题:

我到底该怎么做?我想我对以下代码非常接近,但无论我做什么 - 而且我确信一定有一些我没有做对的地方 - 它总是只打开外部存储中的主文件夹.

How exactly do I do that? I think I came quite close with the following code, but no matter what I do - and I'm certain that there must be something I didn't get right yet - it always opens only the main folder from the external storage.

public void openFolder()
{
File file = new File(Environment.getExternalStorageDirectory(),
    "myFolder");

Log.d("path", file.toString());

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(Uri.fromFile(file), "*/*");
startActivity(intent);
}

推荐答案

我终于让它工作了.这样,选择器只会显示少数应用程序(Google Drive、Dropbox、Root Explorer 和 Solid Explorer).它在两个资源管理器上运行良好,但不适用于 Google Drive 和 Dropbox(我猜是因为它们无法访问外部存储).其他 MIME 类型如 "*/*" 也是可能的.

I finally got it working. This way only a few apps are shown by the chooser (Google Drive, Dropbox, Root Explorer, and Solid Explorer). It's working fine with the two explorers but not with Google Drive and Dropbox (I guess because they cannot access the external storage). The other MIME type like "*/*" is also possible.

public void openFolder(){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
         +  File.separator + "myFolder" + File.separator);
    intent.setDataAndType(uri, "text/csv");
    startActivity(Intent.createChooser(intent, "Open folder"));
}

这篇关于Android:如何通过 Intent 打开特定文件夹并在文件浏览器中显示其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆