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

问题描述

有谁知道一个完整的文件选择对话框?也许有,你可以过滤掉所有的文件,除了那些具有特定扩展名?互联网需要这样的一个例子。我还没有发现任何重量足够轻落实轻松插入我的项目。唯一的其他选择似乎用OI FileManger开放的意图是,但是这需要已经有安装文件管理器用户。

Does anyone know of a complete choose file dialog? Maybe one where you can filter out all files except for ones with specific extensions? The internet needs such an example. I have not found anything lightweight enough to implement easily into on of my projects. The only other options seem to being using OI FileManger's open intents, but that requires the user already having the file manager installed.

我将非常感激,如果有人能指出一个对话框,允许用户浏览文件夹并选择一个文件,并返回路径。

I would be extremely grateful if someone could point out a Dialog that would allow the user to browse folders and select a file, and return the path.

推荐答案

那么,如果你是我的短期工与代表:D你只需要重写onCreateDialog在活动

Well if you are temping me with rep :D You just need to override onCreateDialog in an activity.

//In an Activity
private String[] mFileList;
private File mPath = new File(Environment.getExternalStorageDirectory() + "//yourdir//");
private String mChosenFile;
private static final String FTYPE = ".txt";
private static final int DIALOG_LOAD_FILE = 1000;

private void loadFileList() {
    try {
        mPath.mkdirs();
    }
    catch(SecurityException e) {
        Log.e(TAG, "unable to write on the sd card " + e.toString());
    }
    if(mPath.exists()) {
        FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File dir, String filename) {
                File sel = new File(dir, filename);
                return filename.contains(FTYPE) || sel.isDirectory();
            }

        };
        mFileList = mPath.list(filter);
    }
    else {
        mFileList= new String[0];
    }
}

protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    AlertDialog.Builder builder = new Builder(this);

    switch(id) {
        case DIALOG_LOAD_FILE:
            builder.setTitle("Choose your file");
            if(mFileList == null) {
                Log.e(TAG, "Showing file picker before loading the file list");
                dialog = builder.create();
                return dialog;
            }
            builder.setItems(mFileList, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    mChosenFile = mFileList[which];
                    //you can do stuff with the file here too
                }
            });
            break;
    }
    dialog = builder.show();
    return dialog;
}

希望这有助于!

Hope this helps!

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

07-30 08:28