也发一个自己实现的android简单文件选择器代码。支持多卡,排序
一个很简单的文件选择器对话框,支持双sd卡,当然前提是要有sd卡。。并且实现了排序效果.
只有100多行的代码,基本的思路就是用listview显示目录下的所有子文件,再判断是文件还是目录。
利用Collections.sort方法进行排序。
代码如下:
1 package net.yunstudio.demo; 2 3 import java.io.File; 4 import java.text.Collator; 5 import java.util.ArrayList; 6 import java.util.Collections; 7 import java.util.Comparator; 8 import java.util.HashMap; 9 import java.util.List; 10 import java.util.Locale; 11 import java.util.Map; 12 13 14 import android.app.Dialog; 15 import android.content.Context; 16 import android.os.Environment; 17 import android.text.TextUtils; 18 import android.util.Log; 19 import android.view.View; 20 import android.widget.AdapterView; 21 import android.widget.AdapterView.OnItemClickListener; 22 import android.widget.ListView; 23 import android.widget.SimpleAdapter; 24 25 public class FilePickerDialog extends Dialog implements OnItemClickListener{ 26 27 public FilePickerDialog(Context context) { 28 super(context); 29 init(); 30 } 31 32 33 private ListView lv_file; 34 private SimpleAdapter simpleAdapter; 35 private List<Map<String, Object>> dataList; 36 private OnFileSelectListener listener; 37 //通过获取sd卡的父级,可以兼容双sd卡的情况 38 private static final File rootPath= 39 Environment.getExternalStorageDirectory().getParentFile(); 40 41 private static final String SRC="src"; 42 private static final String NAME="name"; 43 private static final String FILE="file"; 44 private static final String IS_BAK="isBak"; 45 46 private void init() { 47 getChildren2dataList(rootPath); 48 simpleAdapter=new SimpleAdapter(getContext(),dataList , R.layout.list_item_choosefile, 49 new String[]{SRC,NAME}, new int[]{R.id.iv_file_type,R.id.tv_file_name}); 50 51 lv_file=new ListView(getContext()); 52 lv_file.setAdapter(simpleAdapter); 53 lv_file.setOnItemClickListener(this); 54 setContentView(lv_file); 55 56 setCanceledOnTouchOutside(true); 57 this.setTitle("请选择文件"); 58 } 59 /** 60 * 设置选择文件的监听器 61 * @param listener 62 */ 63 public void setOnFileSelectListener(OnFileSelectListener listener) { 64 this.listener = listener; 65 } 66 /** 67 * 将本目录的子文件的信息赋值给dataList字段 68 * @param dir 69 */ 70 private void getChildren2dataList(File dir) { 71 File[] children=dir.listFiles(); 72 if(dataList==null){ 73 dataList=new ArrayList<Map<String,Object>>(); 74 }else { 75 dataList.clear(); 76 } 77 78 if(!rootPath.equals(dir)){ 79 Map<String, Object> map=new HashMap<String, Object>(); 80 map.put(SRC, R.drawable.folder); 81 map.put(NAME, ".."); 82 map.put(FILE, dir.getParentFile()); 83 map.put(IS_BAK, true); 84 dataList.add(map); 85 } 86 if(children==null) return ;//避免空目录引起的空指针错误 87 for (File file : children) { 88 Map<String, Object> map=new HashMap<String, Object>(); 89 map.put(SRC, file.isDirectory() 90 ?R.drawable.folder 91 :R.drawable.file); 92 map.put(NAME, file.getName()); 93 map.put(FILE, file); 94 dataList.add(map); 95 } 96 97 98 Collections.sort(dataList, new Comparator<Map<String, Object>>() { 99 100 @Override 101 public int compare(Map<String, Object> lhs, Map<String, Object> rhs) { 102 int result=0; 103 104 if(lhs.get(IS_BAK)!=null){ 105 result = -1; 106 }else if (rhs.get(IS_BAK)!=null) { 107 result = 1; 108 }else { 109 File lhsFile=(File) lhs.get(FILE); 110 File rhsFile=(File) rhs.get(FILE); 111 112 if(lhsFile.isDirectory()&&rhsFile.isFile()){ 113 result = -1; 114 }else if (lhsFile.isFile()&&rhsFile.isDirectory()) { 115 result = 1; 116 }else { 117 Collator cmp = Collator.getInstance(Locale.getDefault()); 118 result = cmp.compare(lhs.get(NAME), rhs.get(NAME)); 119 } 120 } 121 return result; 122 } 123 }); 124 } 125 126 @Override 127 public void onItemClick(AdapterView<?> parent, View view, int position, 128 long id) { 129 File file=(File)dataList.get(position).get(FILE); 130 if(file.isDirectory()){ 131 getChildren2dataList(file); 132 simpleAdapter.notifyDataSetChanged(); 133 }else { 134 if(listener!=null){ 135 listener.onFileSelect(file); 136 this.dismiss(); 137 } 138 } 139 } 140 141 public static interface OnFileSelectListener{ 142 void onFileSelect(File file); 143 } 144 }
项目下载地址:点击下载