如何在android中打开文件浏览器?

use*_*495 12 android file-browser

好的,是的,我知道这个问题之前已经在这里提出,但就像2年前一样,情况可能已经改变了.2年是技术领域的长期发展.

我是新手,我正在开发一个Android 4.0.3设备.如何为我的应用打开文件浏览器?android SDK中是否内置了一个?我必须自己写吗?

我不希望我的应用程序依赖于用户安装单独的应用程序进行文件浏览.

Mga*_*erz 18

要从文件浏览器获取文件,请使用以下命令:

        Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
        fileintent.setType("gagt/sdf");
        try {
            startActivityForResult(fileintent, PICKFILE_RESULT_CODE);
        } catch (ActivityNotFoundException e) {
            Log.e("tag", "No activity can handle picking a file. Showing alternatives.");
        }
Run Code Online (Sandbox Code Playgroud)

我不太清楚gagt/sdf的用途......它似乎适用于我的应用程序中的任何文件.

然后添加此方法:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Fix no activity available
        if (data == null)
            return;
        switch (requestCode) {
        case PICKFILE_RESULT_CODE:
            if (resultCode == RESULT_OK) {
                String FilePath = data.getData().getPath();
                //FilePath is your file as a string
            }
        }
Run Code Online (Sandbox Code Playgroud)

如果用户没有安装或预安装OEM的文件管理器应用程序,则必须实现自己的应用程序.你不妨给他们一个选择.


pro*_*oFX 5

我希望这个能帮助你选择文件:

public void performFileSearch() {

    // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
    // browser.
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

    // Filter to only show results that can be "opened", such as a
    // file (as opposed to a list of contacts or timezones)
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Filter to show only images, using the image MIME data type.
    // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
    // To search for all documents available via installed storage providers,
    // it would be "*/*".
    intent.setType("image/*");

    startActivityForResult(intent, READ_REQUEST_CODE);
}
Run Code Online (Sandbox Code Playgroud)

代码来自这个文档:
https : //developer.android.com/guide/topics/providers/document-provider.html
参考它了解更多信息。


poi*_*oae 0

没有一个文件管理应用程序可以在所有设备上安装。您可能希望您的应用程序也能在 Android 3.x 或更低版本的设备上运行。不过,最好的选择是编写自己的文件管理器。这并不像听起来那么费力,网上已经有很多这方面的代码。