如何在Android中打开文件浏览器? [英] How to open a file browser in Android?

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

问题描述

我正在使用Android 4.0.3设备进行开发.如何为我的应用程序打开文件浏览器? Android SDK内置了一个吗?我需要自己写吗?

I am developing on a Android 4.0.3 device. How do I open a file browser for my app? Is there one built in the to Android SDK? Do I have to write my own?

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

I don't want my app to depend on a the user installing a separate app for file browsing.

推荐答案

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

To get a file from a file browser, use this:

        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.");
        }

我不确定gagt/sdf的用途是什么...它似乎可以在我的应用中处理任何文件.

I'm not quite sure what the gagt/sdf is for... it seems to work in my app for any file.

然后添加此方法:

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
            }
        }

如果用户的OEM未安装或未预安装文件管理器应用,则您必须实现自己的文件管理器.您最好给他们一个选择.

If the user doesn't have a file manager app installed or preinstalled by their OEM you're going to have to implement your own. You might as well give them a choice.

这篇关于如何在Android中打开文件浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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