Android的文件选择

Android的文件选择

本文介绍了Android的文件选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个文件上传。而我所以我需要一个文件选择,但我不希望由我自己来写这个。我觉得OI文件管理器,我觉得很适合我。但是,我怎么能强迫用户安装OI文件管理器?如果我不能,有没有更好的办法,包括在我的应用程序文件管理器?THX

I want to make a file uploader. And I hence I need a file chooser but I don't want to write this by myself. I find OI file manager and I think it suits me.But how can I force user to install OI file manager?If I cannot , is there a better way to include a file manager in my app?Thx

推荐答案

修改 2012 1月2 的):

我创建了一个小型的开源的Andr​​oid库项目,简化了这一过程,同时还提供了内置文件浏览器(如果用户不具有一个present)。这是非常简单的使用,要求code只有几行。

I created a small open source Android Library Project that streamlines this process, while also providing a built-in file explorer (in case the user does not have one present). It's extremely simple to use, requiring only a few lines of code.

您可以在GitHub上找到它: aFileChooser

You can find it at GitHub: aFileChooser.

原始的

如果您希望用户可以选择系统中的任何文件,您将需要包括你自己的文件管理器,或者建议用户下载一个。我认为最好的你能做的就是寻找在开的内容的 Intent.createChooser()是这样的:

If you want the user to be able to choose any file in the system, you will need to include your own file manager, or advise the user to download one. I believe the best you can do is look for "openable" content in an Intent.createChooser() like this:

private static final int FILE_SELECT_CODE = 0;

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                FILE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Please install a File Manager.",
                Toast.LENGTH_SHORT).show();
    }
}

然后,您可以监听所选文件的乌里 onActivityResult()像这样:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case FILE_SELECT_CODE:
        if (resultCode == RESULT_OK) {
            // Get the Uri of the selected file
            Uri uri = data.getData();
            Log.d(TAG, "File Uri: " + uri.toString());
            // Get the path
            String path = FileUtils.getPath(this, uri);
            Log.d(TAG, "File Path: " + path);
            // Get the file instance
            // File file = new File(path);
            // Initiate the upload
        }
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

getPath()方法我 FileUtils.java 是:

public static String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

这篇关于Android的文件选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 16:31