Android 文件浏览器总结
jopen
10年前
文件浏览器用来读取android系统中的所有文件和文件夹。具体说明如下:
-
最上面显示当前的文件路径。如果是根目录,则显示“根目录”;
-
第二行是返回上一级按钮。如果当前处于根目录下,则该行不显示;
-
文件显示使用listView控件,所有文件一次性加载完毕;若当前是文件夹,则可点击,进入下一级目录,若是文件,则点击,默认为选中该文件。返回文件名,并关闭文件浏览器。
例:
文件浏览器Activity
public class FileManagerActivity extends Activity { private TextView mCurrentPath; private TextView mReturn; private ListView mList; private View mPathLine; private String mReturnPath = null; private FileManagerAdapter adapter; private ArrayList<Map<String, Object>> infos = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.file_list); initView(); } private void initView() { mCurrentPath = (TextView) findViewById(R.id.file_path); mPathLine = findViewById(R.id.file_path_line); mReturn = (TextView) findViewById(R.id.file_return); mList = (ListView) findViewById(R.id.file_list); mList.setOnItemClickListener(clickListener); mReturn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String returnStr = mReturn.getText().toString(); if (mReturnPath.length() > 0 && returnStr.equals("返回上一级")) { initList(mReturnPath); } } }); initList("/"); //初始化从根目录开始 } private void initList(String path) { File file = new File(path); File[] fileList = file.listFiles(); infos = new ArrayList<Map<String, Object>>(); Map<String, Object> item = new HashMap<String, Object>(); Drawable drawable; if (path.equals("/")) { //如果当前为根目录,返回上一级按钮,不显示 drawable = getResources().getDrawable(R.drawable.versionup); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); mCurrentPath.setCompoundDrawablePadding(10); mCurrentPath.setCompoundDrawables(drawable, null, null, null); mCurrentPath.setText("根目录列表"); mReturnh.setVisibility(View.GONE); mPathLine.setVisibility(View.GONE); } else { drawable = getResources().getDrawable(R.drawable.versionup); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); mReturn.setCompoundDrawables(drawable, null, null, null); mReturn.setText("返回上一级"); mReturnPath = file.getParent(); //保存该级目录的上一级路径 mCurrentPath.setVisibility(View.VISIBLE); mPathLine.setVisibility(View.VISIBLE); mCurrentPath.setText(file.getPath()); } try { for (int i = 0; i < fileList.length; i++) { item = new HashMap<String, Object>(); File fileItem = fileList[i]; if (fileItem.isDirectory()) { //如果当前文件为文件夹,设置文件夹的图标 item.put("icon", R.drawable.icon_one); } else item.put("icon", R.drawable.icon_two); item.put("name", fileItem.getName()); item.put("path", fileItem.getAbsolutePath()); infos.add(item); } } catch (Exception e) { e.printStackTrace(); } adapter = new FileManagerAdapter(this); adapter.setFileListInfo(infos); mList.setAdapter(adapter); } private OnItemClickListener clickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { File file = new File((String) (infos.get(position).get("path"))); if (file.isDirectory()) { //若点击文件夹,则进入下一级目录 String nextPath = (String) (infos.get(position).get("path")); initList(nextPath); } else { //若点击文件,则将文件名发送至调用文件浏览器的主界面 Intent intent = new Intent(); intent.setClass(FileManagerActivity.this, A.class); intent.putExtra("fileName", (String) (infos.get(position).get("name"))); intent.putExtra("path", (String) (infos.get(position).get("path"))); setResult(RESULT_OK, intent); finish(); } } }; }
文件浏览器的adapter
public class FileManagerAdapter extends BaseAdapter{ private Context mContext; private List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); public FileManagerAdapter(Context context) { super(); mContext = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int position, View convertView, ViewGroup arg2) { FileMangerHolder holder; if(null == convertView){ holder = new FileMangerHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.file_item, null); holder.icon = (ImageView) convertView.findViewById(R.id.file_item_icon); holder.name = (TextView) convertView.findViewById(R.id.file_item_name); convertView.setTag(holder); }else{ holder = (FileMangerHolder) convertView.getTag(); } holder.icon.setImageResource((Integer)(list.get(position).get("icon"))); holder.name.setText((String)(list.get(position).get("name"))); return convertView; } public class FileMangerHolder{ public ImageView icon; public TextView name; } public void setFileListInfo(List<Map<String, Object>> infos){ list.clear(); list.addAll(infos); notifyDataSetChanged(); } }
file_list.layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black" android:orientation="vertical" > <TextView android:id="@+id/file_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:textColor="@color/white" android:textSize="12sp" /> <View android:id="@+id/file_path_line" android:layout_width="match_parent" android:layout_height="0.3dp" android:background="@color/gray_dark" /> <TextView android:id="@+id/file_return" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:textColor="@color/white" android:textSize="12sp" /> <View android:layout_width="match_parent" android:layout_height="0.3dp" android:background="@color/gray_dark" /> <ListView android:id="@+id/file_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:cacheColorHint="@color/transparent" android:divider="@color/gray_dark" android:dividerHeight="0.3dp" android:listSelector="@null" android:scrollbars="none" /> </LinearLayout>
file_item.layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal" > <ImageView android:id="@+id/file_item_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" /> <TextView android:id="@+id/file_item_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:textColor="@color/white" android:textSize="12sp" /> </LinearLayout>
来自:http://my.oschina.net/u/1858156/blog/312069