Android文件选择器

来源:互联网 发布:机顶盒vip破解软件 编辑:程序博客网 时间:2024/06/05 05:07

今天跟大家分享一下Android的文件选择器,在我们的日常开发中可能需要用到文件选择的需求,而调用Android系统原生的文件选择在部分手机上可能会有获取不到文件路径的问题,那么我们可以通过自己实现文件选择器的方式来解决这个问题。

思路是这样滴:

1:首先拿到sd卡的根目录,然后通过listview来将根目录的文件展示给用户,当用户点击listview某一项时判断该项对应的文件是不是目录,是目录的话就进入该目录将该目录下的文件同样展示在listview中,如果不是目录的话则表示用户选中该文件,此时我们要将选中文件的路径取出并回传给调用处。

2:我们要监听返回按钮的点击事件,在用户点击了返回键时判断当前处于文件系统的哪个层级,如果不是根目录的话则返回上一级,如果是处于根目录的话则表示用户取消文件选择,我们将文件选择页面销毁。

废话不说了,下面 :小二!上代码! 

public classPickFileActivity extends Activity {

public static final int PICK_FILE_RESULT_CODE = 0;// 结果码

private String rootPath;//根路径

private String currentPath;//当前路径

privateListView lvFiles;

private List<File> files;//文件列表

private FileAdapter fileAdapter;//文件适配器

privateItemClickListener itemClickListener;

private File currentFile;//当前选中的文件

privateTextView tvEmpty;

 

@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_file_pick);

initWidget();

initData();

}

 

/**

 *初始化View组件

 */

privatevoid initWidget() {

tvEmpty= (TextView) findViewById(R.id.tv_empty);

lvFiles= (ListView) findViewById(R.id.lv_files);

lvFiles.setEmptyView(tvEmpty);

itemClickListener= new ItemClickListener();

lvFiles.setOnItemClickListener(itemClickListener);

}

 

/**

 *初始化相关数据

 */

privatevoid initData() {

files= new ArrayList<File>();

fileAdapter= new FileAdapter(PickFileActivity.this, files);

lvFiles.setAdapter(fileAdapter);

rootPath= Environment.getExternalStorageDirectory().getAbsolutePath();

updateFiles(rootPath);

}

 

/**

 *设置listview适配器

 * @param path

 */

privatevoid updateFiles(String path) {

currentPath= path;

Filefile = new File(path);

if(file.exists()) {

if(file.isDirectory()) {

// path路径的文件为目录时,添加子文件

files.clear();

File[]childFiles = file.listFiles();

for(File tempFile : childFiles) {

if(!tempFile.isHidden()) {

files.add(tempFile);

}

}

}else {

// path路径的文件为非目录时

files.clear();

files.add(file);

}

fileAdapter.notifyDataSetChanged();

}

}

 

/**

 *返回键监听

 */

@Override

publicvoid onBackPressed() {

//当前路径为根路径时退出,否则返回上一层

if(currentPath.equals(rootPath)) {

IntentintentData = new Intent();

intentData.putExtra("filePath","");

setResult(0,intentData);

super.onBackPressed();

}else {

FilecurFile = new File(currentPath);

updateFiles(curFile.getParent());

}

}

 

/**

 * listviewitem点击事件

 * @author fanxi

 */

privateclass ItemClickListener implements OnItemClickListener {

@Override

publicvoid onItemClick(AdapterView<?> adapterView, View view,

intposition, long id) {

currentFile= files.get(position);

if(!currentFile.isDirectory()) {

//选中的file不是目录时返回选择结果

IntentintentData = new Intent();

intentData.putExtra("filePath",currentFile.getAbsolutePath());

setResult(0,intentData);

finish();

}else {

//点击的file是目录时则进入目录

updateFiles(currentFile.getAbsolutePath());

}

}

}

}

 

我们这里是通过setResult()的方式来回传文件路径的,所以调用处要使用startActivityForResult()来切换到文件选择页面。我们的主要逻辑和代码就这些,只是简单的实现了文件选择的功能,功能和美观度都需要改善。小二已奉上源码,欢迎大家吐槽。

源码链接:http://download.csdn.net/detail/u011282703/9595396

2 0
原创粉丝点击