基于SD卡得文件选择器

[/code][code="java"]◦public class ActivityFilePicker extends Activity {   

◦ private ListView listViewFilePicker;
◦ private File mCurrentDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()); //根目录位置
◦ AdapterFilePicker mFileAdapter; //ListView适配器
◦ String fileEndings[] = { "mp3" }; //显示的文件类型

◦ @Override
◦ public void onCreate(Bundle savedInstanceState) {
◦ super.onCreate(savedInstanceState);
◦ setContentView(R.layout.layout_activityfilepicker);
◦ listViewFilePicker = (ListView) findViewById(R.id.listview);
◦ listViewFilePicker.setCacheColorHint(0x00000000);
◦ mFileAdapter = new AdapterFilePicker(this);
◦ listViewFilePicker.setAdapter(mFileAdapter);
◦ //Item点击事件
◦ ListView.OnItemClickListener lv2click = new ListView.OnItemClickListener() {
◦ public void onItemClick(AdapterView parent, View view,
◦ int position, long id) {
◦ int fid = mFileAdapter.getItemType((int) id);
◦ String mPath = "";
◦ if (fid == 1) { //文件类型1为文件夹,点击进入该文件夹目录
◦ String s1 = mFileAdapter.getItem((int) id).name;
◦ if (s1.equals("..")) {
◦ mPath = mCurrentDirectory.getParent();
◦ } else {
◦ mPath = mCurrentDirectory.getPath() + "/" + s1 + "/";
◦ }
◦ mCurrentDirectory = new File(mPath);
◦ ListFile(mCurrentDirectory);
◦ } else { //返回选中的文件的Path
◦ Bundle bundle = new Bundle();
◦ bundle.putString("path", mCurrentDirectory.getPath()
◦ + "/" + mFileAdapter.getItem((int) id).name);
◦ Intent mIntent = new Intent();
◦ mIntent.putExtras(bundle);
◦ setResult(RESULT_OK, mIntent);
◦ ActivityFilePicker.this.finish();
◦ }
◦ }
◦ };
◦ ListFile(mCurrentDirectory);
◦ listViewFilePicker.setOnItemClickListener(lv2click);
◦ }

◦ /**
◦ * 列出该目录的文件
◦ * @param aDirectory
◦ */
◦ private void ListFile(File aDirectory) {
◦ mFileAdapter.clearItems();
◦ mFileAdapter.notifyDataSetChanged();
◦ listViewFilePicker.postInvalidate();
◦ if (!aDirectory.getPath().equals("/sdcard")) {
◦ FileData fd = new FileData();
◦ fd.name = "..";
◦ fd.type = 1;
◦ mFileAdapter.addItem(fd);
◦ }
◦ for (File f : aDirectory.listFiles()) {
◦ if (f.isDirectory()) {
◦ FileData fd = new FileData();
◦ fd.name = f.getName();
◦ fd.type = 1;
◦ mFileAdapter.addItem(fd);
◦ } else {
◦ if (checkEnds(f.getName().toLowerCase())) {
◦ FileData fd = new FileData();
◦ fd.name = f.getName();
◦ fd.type = 0;
◦ mFileAdapter.addItem(fd);
◦ }
◦ }
◦ }
◦ mFileAdapter.notifyDataSetChanged();
◦ listViewFilePicker.postInvalidate();
◦ }

◦ private boolean checkEnds(String checkItsEnd) {
◦ for (String aEnd : fileEndings) {
◦ if (checkItsEnd.endsWith(aEnd))
◦ return true;
◦ }
◦ return false;
◦ }
◦}


由于模块结构很简单,所以把遍历文件的方法放在Activity上了

◦public class AdapterFilePicker extends BaseAdapter {   
◦ private Context mContext;
◦ private Vector mItems = new Vector();
◦ private LinearLayout layout, layout_more;
◦ private LayoutInflater inflate;
◦ private TextView textViewItem;

◦ public AdapterFilePicker(Context context) {
◦ mContext = context;
◦ inflate = (LayoutInflater) mContext
◦ .getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
◦ }

◦ public void addItem(FileData item) {
◦ mItems.add(item);
◦ }

◦ public FileData getItem(int it) {
◦ return (FileData) mItems.elementAt(it);
◦ }

◦ public int getCount() {
◦ return mItems.size();
◦ }

◦ public long getItemId(int arg0) {
◦ return arg0;
◦ }

◦ public int getItemType(int arg0) {
◦ return getItem(arg0).type;
◦ }

◦ public void clearItems() {
◦ mItems.clear();
◦ }

◦ public View getView(int position, View view, ViewGroup arg2) {
◦ if(null == view){
◦ view = (LinearLayout) inflate.inflate(R.layout.item_filepicker_listview, null);
◦ }
◦ textViewItem = (TextView) view.findViewById(R.id.textViewFileName);
◦ textViewItem.setText(getItem(position).name);
◦ return view;
◦ }

◦}


◦/**
◦ * 文件结构
◦ * @author Administrator
◦ *
◦ */
◦public class FileData {
◦ public String name; //文件名
◦ public int type; //文件类型
◦}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值