介绍一个android开源文件选择对话框:android-file-dialog

链接见此:http://code.google.com/p/android-file-dialog/

源码在这里:http://code.google.com/p/android-file-dialog/source/browse/#svn%2Ftrunk%2FFileExplorer%2Fsrc%2Fcom%2Flamerman

谢谢作者的无私贡献,虽然功能简单,但能节省些许开发时间。同时还能应付一些常见的文件选择功能需求,大家可根据自己的项目需求在上面作相应更改。


使用说明:

Usage principles

The dialog is created like this:

Intent intent = new Intent(getBaseContext(), FileDialog.class);
        intent.putExtra(FileDialog.START_PATH, "/sdcard");
        
        //can user select directories or not
        intent.putExtra(FileDialog.CAN_SELECT_DIR, true);
        
        //alternatively you can set file filter
        //intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "png" });
        
        startActivityForResult(intent, REQUEST_SAVE);

Here "/sdcard" is the start path where the file dialog will open. REQUEST_SAVE is your integer constant, it will then be transmitted to onActivityResult;

CAN_SELECT_DIR - possibility to select directories FORMAT_FILTER - files filter. If file name ends with one of filters the file will be displayed.

When user selects a file or just goes back not selected anything, the activity closes, calling the standard onActivityResult method:

    public synchronized void onActivityResult(final int requestCode,
        int resultCode, final Intent data) {

        if (resultCode == Activity.RESULT_OK) {

            if (requestCode == REQUEST_SAVE) {
                System.out.println("Saving...");
            } else if (requestCode == REQUEST_LOAD) {
                System.out.println("Loading...");
            }
            
            String filePath = data.getStringExtra(FileDialog.RESULT_PATH);

        } else if (resultCode == Activity.RESULT_CANCELED) {
            Logger.getLogger(AccelerationChartRun.class.getName()).log(
                    Level.WARNING, "file not selected");
        }

    }

The result code will be Activity.RESULT_OK if user has selected something and Activity.RESULT_CANCELED if he just pressed the BACK button not selecting anything. You can retrieve the filepath from the intent:

    data.getStringExtra(FileDialog.RESULT_PATH);

If for some reason the words startActivityForResult and onActivityResult are not familiar to you, just read documentation about them. In short, an activity is created and when it exits a handler is called. In this handler you can get the filepath.

You can add one more parameter to the Intent: SelectionMode.MODE_OPEN or SelectionMode.MODE_CREATE

intent.putExtra(FileDialog.SELECTION_MODE, SelectionMode.MODE_OPEN);

SelectionMode.MODE_OPEN will make the NEW button disabled. So user can only select existing files



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值