【问题标题】:Android: Directory and file chooser android libraryAndroid:目录和文件选择器android库
【发布时间】:2014-05-02 09:29:25
【问题描述】:

我在我的应用程序中使用 FileChooser android 库项目从外部存储中选择文件。但它似乎并没有只选择目录让用户选择下载位置来下载文件。 有没有支持pick文件和pick目录的android库项目?

我知道这里已经回答了多个关于文件选择器或目录选择器的问题,但经过广泛搜索后,我找不到目录和文件选择器的问题。 任何帮助将不胜感激。

【问题讨论】:

    标签: android picker filechooser


    【解决方案1】:

    我没有 android 库项目,但您可以使用下一个代码简单地制作自己的文件选择器。此代码将要求您选择文件浏览器,当您在文件浏览器中选择文件时,您将在 FilePath 字符串中的 onActivityResult 函数中获得路径。

    创建这个公众:

    private static final int ACTIVITY_CHOOSE_FILE = 3;
    

    当一个按钮被点击时,你可以这样调用:

                Intent chooseFile;
                Intent intent;
                chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
                chooseFile.setType("file/*");
                intent = Intent.createChooser(chooseFile, "Choose a file");
                startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
    

    您可以使用此代码捕获目录:

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode != RESULT_OK) return;
            String path     = "";
            if(requestCode == ACTIVITY_CHOOSE_FILE)
            {
                  Uri uri = data.getData();
                  String FilePath = getRealPathFromURI(uri);
    
            }
        }
    
    public String getRealPathFromURI(Uri contentUri) {
        String [] proj      = {MediaStore.Images.Media.DATA};
        Cursor cursor       = getContentResolver().query( contentUri, proj, null, null,null); 
        if (cursor == null) return null; 
        int column_index    = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    

    编辑: 如果您不想使用外部文件浏览器,可以将这个 android 库导入到您的项目中: https://code.google.com/p/afiledialog/

    【讨论】:

    • 感谢 DeGoosseZ 提供您的代码...但如果用户没有文件浏览器?
    • 当没有文件浏览器时,此方法不起作用。现在我不知道可以做到这一点的android库项目。我会对此进行一些研究。
    • 我还没有测试过这个,但它似乎有一个很好的“操作方法”:code.google.com/p/afiledialog
    • 是的,这看起来不错……我会试试看。非常感谢 DeGoosseZ
    • 请将其标记为其他人的答案。不客气!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 2011-12-13
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    相关资源
    最近更新 更多