选择文件对话框 [英] Choose File Dialog

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

问题描述

有没有人知道一个完整的选择文件对话框?也许你可以滤除除特定扩展名之外的所有文件?



我没有发现任何轻量级的东西,轻松实现我的一个项目。唯一的其他选项似乎是使用OI FileManager的打开意图,但这要求用户已经安装了文件管理器。



如果有人可以指出允许用户浏览文件夹并选择文件并返回路径的对话框,我将不胜感激。

解决方案

您只需在活动中覆盖 onCreateDialog

  //在活动中
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,无法在sd卡上写+ 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对话框onCreateDialog(int id){
对话框对话框= null;
AlertDialog.Builder builder = new Builder(this);

switch(id){
case DIALOG_LOAD_FILE:
builder.setTitle(选择你的文件);
if(mFileList == null){
Log.e(TAG,在加载文件列表之前显示文件选择器);
dialog = builder.create();
返回对话框;
}
builder.setItems(mFileList,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
mChosenFile = mFileList [which];
//你可以在这里做文件的东西
}
});
break;
}
dialog = builder.show();
返回对话框;
}


Does anyone know of a complete choose file dialog? Maybe one where you can filter out all files except for ones with specific extensions?

I have not found anything lightweight enough to implement easily into one of my projects. The only other option seems to be using OI FileManager's open intents, but that requires the user already having the file manager installed.

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

解决方案

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

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

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