Android文件选择器

时间:2022-03-01 14:51:02

直接给代码,自己慢慢钻研吧

FileSelectorActivity.java

    private ListView lv2;
private File mCurrentDirectory = new File("/sdcard/");
private ExpBaseAdapter ap;

private String medias[] = {"bmp", "jpg", "gif", "jpeg", "avi", "mp4", "3pg", "mpg", "dat", "rmvb", "wav", "mp3"};
private String docs[] = {"doc", "docx", "wps", "txt", "pdf", "rtf"};
private String compressPackages[] = {"zip", "rar", "tar", "gz", "7z", "iso"};


public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setTitle("File Selector");
setContentView(R.layout.activity_file_selector);

//文件列表
lv2 = (ListView) findViewById(R.id.of_lv);
lv2.setCacheColorHint(0x00000000);
ap = new ExpBaseAdapter(this);
lv2.setAdapter(ap);

ListView.OnItemClickListener lv2click = new ListView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
int fid = ap.getItemType((int) id);
String mPath = "";

if (fid == 1 || fid == 5) { //文件夹点击后继续显示子文件
String s1 = ap.getItem((int) id).name;
if (s1.equals("..")) {
mPath = mCurrentDirectory.getParent();
} else {
mPath = mCurrentDirectory.getPath() + "/" + s1 + "/";
}

mCurrentDirectory = new File(mPath);
ListFile(mCurrentDirectory);
} else { //选中文件传回文件地址
Bundle bundle = new Bundle();
bundle.putString("filename", mCurrentDirectory.getPath()
+ "/" + ap.getItem((int) id).name);
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
FileSelectorActivity.this.finish();
}
}

};
ListFile(mCurrentDirectory);
lv2.setOnItemClickListener(lv2click);
}

//列出文件和文件夹
private void ListFile(File aDirectory) {
ap.clearItems();
ap.notifyDataSetChanged();
lv2.postInvalidate();
Log.v("vodone", "mpath=" + aDirectory.getPath());

// if (aDirectory==null)return;

if (!aDirectory.getPath().equals("/sdcard")) {
FileData fd = new FileData();
fd.name = "..";
fd.type = 5;
ap.addItem(fd);
}

for (File f : aDirectory.listFiles()) {
if (f.isDirectory()) {
if(!f.getName().startsWith(".")) {
FileData fd = new FileData();
fd.name = f.getName();
fd.type = 1;
ap.addItem(fd);
}
} else {
// 显示特定类型文件
// if (checkEnds(f.getName().toLowerCase())) {
// fileData fd = new fileData();
// fd.name = f.getName();
// fd.type = 0;
// ap.addItem(fd);
// }

//显示全部
FileData fd = new FileData();
fd.name = f.getName();
fd.type = checkType(f.getName().toLowerCase());
ap.addItem(fd);
}
}

ap.notifyDataSetChanged();
lv2.postInvalidate();

}

// 显示特定类型文件
//private boolean checkEnds(String checkItsEnd) {
//for (String aEnd : fileEndings) {
//if (checkItsEnd.endsWith(aEnd))
//return true;
//}
//return false;
//}

//分类设置类型
private int checkType(String fileName) {
//type类型说明:
//0 file, 1 folder, 2 package, 3 meida, 4 doc
for(String compressPackage : compressPackages) {
if(fileName.endsWith(compressPackage))
return 2;
}
for(String media : medias) {
if(fileName.endsWith(media))
return 3;
}
for(String doc : docs) {
if(fileName.endsWith(doc))
return 4;
}
return 0;
}

ExpBaseAdapter.java

public class ExpBaseAdapter extends BaseAdapter {

private Context mContext;

private Vector mItems = new Vector();

private LinearLayout layout, layout_more;

public ExpBaseAdapter(Context context) {

mContext = context;

}

public void addItem(FileData it) {

mItems.add(it);

}

public FileData getItem(int it) {

return (FileData) mItems.elementAt(it);

}

public int getCount() {

// TODO Auto-generated method stub

return mItems.size();

}

public long getItemId(int arg0) {

// TODO Auto-generated method stub

return arg0;

}

public int getItemType(int arg0) {

// TODO Auto-generated method stub

return getItem(arg0).type;

}

public void clearItems() {

mItems.clear();

}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

// TODO Auto-generated method stub

LayoutInflater inflate = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

arg1 = (LinearLayout) inflate.inflate(R.layout.item_list_file, null);

ImageView fm_image = (ImageView) arg1.findViewById(R.id.fm_image);

//根据不同类型设置图标
 switch (getItemType(arg0)) {
case 0:
fm_image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.file));
break;
case 1:
fm_image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.folder));
break;
case 2:
fm_image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.compress_package));
break;
case 3:
fm_image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.media));
break;
case 4:
fm_image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.docs));
break;
case 5:
fm_image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.back));
break;
}

TextView fm_text_01 = (TextView) arg1.findViewById(R.id.fm_text_01);
fm_text_01.setText(getItem(arg0).name);

return arg1;

}

}

文件类型FileData.java

public class FileData {

public String name;

public int type;

}