一个可以浏览选择文件和文件夹的小程序,在开发中经常会用到选择一些文件进行操作的情况,所以就自己写了一个。
先上效果图:
源码下载:http://download.csdn.net/detail/qiang_csd/7456693
主要的类:
SelectFileActivity.java
package com.example.choosefile;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
public class SelectFileActivity extends Activity {
/**
* 总布局
*/
private LinearLayout wholeLayout;
private Context mContext;
public final static int SELECT_FILE_CODE = 1;
/**
* 记录选择的文件夹,用于返回时返回上次的目录
*/
private List<String> allShowFiles = new ArrayList<String>();
/**
* 定义显示文件的图标
*/
private FileIconUtil fileIcon;
/**
* 用于过滤显示的文件,通过intent传递要选择文件的扩展名
*/
private String needExt = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
wholeLayout = new LinearLayout(mContext);
fileIcon=new FileIconUtil();
wholeLayout.setOrientation(LinearLayout.VERTICAL);
wholeLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
initListView(Environment.getExternalStorageDirectory()
.getAbsolutePath());
setContentView(wholeLayout);
Intent postIntent = getIntent();
if (postIntent != null) {
String s = postIntent.getStringExtra("ext");
if (s != null && !s.equalsIgnoreCase("")) {
needExt = s;
}
}
}
/**
* 根据path路径初始化listview
* @param path 根目录路径
*/
private void initListView(String path) {
final List<ImageListDomain> li = new ArrayList<ImageListDomain>();
File root = new File(path);
if (root.isFile()) {//如果根目录是文件
li.add(new ImageListDomain(fileIcon.getFileIcon(getFileExt(root)), root.getName(), root
.getAbsolutePath()));
} else {
//对文件进行排序
String[] filePaths = root.list();
Arrays.sort(filePaths, new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
//忽略大小写
return lhs.toLowerCase().compareTo(rhs.toLowerCase());
}
});
for (String fp : filePaths) {
File f = new File(root.getAbsolutePath() + "/" + fp);
if (f.isDirectory()) {
li.add(new ImageListDomain(fileIcon.getFileIcon("DIC"), f.getName(),
true, f.getAbsolutePath()));
} else {
//只有是符合的扩展名或者没有传递扩展名
if(needExt == null || getFileExt(f).equals(needExt)){
li.add(new ImageListDomain(fileIcon.getFileIcon(getFileExt(f).toLowerCase()), f.getName(), f
.getAbsolutePath()));
}
}
}
}
ListView listView = new ListView(mContext);
ShowlistAdapter sa = new ShowlistAdapter(mContext, li,
R.layout.mytextview);
listView.setAdapter(sa);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ImageListDomain d = li.get(position);
if (d.isDic()) {
allShowFiles.add(new File(d.getRootPath()).getParent());
wholeLayout.removeAllViews();//移除所有的view
initListView(d.getRootPath());
} else {//如果选择的是文件就返回文件的路径
Intent intent = new Intent();
intent.putExtra("path", d.getRootPath());
setResult(SELECT_FILE_CODE, intent);
finish();
}
}
});
wholeLayout.addView(listView);
wholeLayout.invalidate();
}
/**
* 获取文件扩展名
* @param file
* @return
*/
private String getFileExt(File file) {
String name = file.getName();
if (name.lastIndexOf(".") >= 0) {
return name.substring(name.lastIndexOf(".") + 1);
} else {
return "";
}
}
/**
* 监听返回键,返回上个目录
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (allShowFiles.size() > 0) {
wholeLayout.removeAllViews();
initListView(allShowFiles.get(allShowFiles.size() - 1));
allShowFiles.remove(allShowFiles.size() - 1);
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
思路和实现都比较简单,也有注释就不多说了。