有时候我们保存文件总会用到文件浏览器功能.那么今天博主带大家做一个。
那么开始,浏览文件,我们就需要写一个文件工具类。
import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.util.Log; public class FileUtils { /** * 获取当前目录下的所有文件或文件夹 * @param path 路径 * @return */ public static List<Map<String,Object>> GetPathFilsList(String path) { List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); List<Map<String,Object>> filelist = new ArrayList<Map<String,Object>>(); try { String[] Files = new File(path).list(); for(String file : Files){ Map<String, Object> map = new HashMap<String, Object>(); if(new File(path+file).isDirectory()){ map.put("isDirectory",2); map.put("fileName", file); list.add(map); }else { map.put("isDirectory", 1); map.put("fileName", file); filelist.add(map); } } list.addAll(filelist); return list; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; } } }
这里会将文件和文件夹区分出来,便于显示区分。
之后我们需要用到一个适配器,用于显示这些数据的ListView
private class FileBrowserAdapter extends BaseAdapter{ private List<Map<String, Object>> fileList; private Context context; public FileBrowserAdapter(Context Context, List<Map<String, Object>> fileList) { this.fileList = fileList; this.context = context; } @Override public int getCount() { return fileList == null ? 0 : fileList.size(); } @Override public Object getItem(int position) { return fileList.get(position); } @Override public long getItemId(int position) { return position; } @SuppressLint("InflateParams") @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater mInflater = LayoutInflater .from(getApplicationContext()); View view = null; view = mInflater.inflate(R.layout.file_list_item, null); ImageView image = (ImageView) view .findViewById(R.id.file_list_item_image); if (Integer .parseInt(fileList.get(position).get("isDirectory") + "") == 2) image.setImageResource(R.drawable.folder); else if (Integer.parseInt(fileList.get(position).get("isDirectory") + "") == 1) image.setImageResource(R.drawable.documents); TextView textView = (TextView) view .findViewById(R.id.file_list_item_testview); textView.setTextColor(Color.BLACK); textView.setText(fileList.get(position).get("fileName") + ""); return view; } }
最后我们需要实现这些
dir = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/"; fileListView = (ListView) findViewById(R.id.file_listview); listItemClickListener = new FileListItemClickListener(); // //设置点击事件 fileListView.setOnItemClickListener(listItemClickListener); fileList = FileUtils.GetPathFilsList(dir); if (new File(dir).getParent() != null) { Map<String, Object> map = new HashMap<String, Object>(); map.put("isDirectory", 0); map.put("fileName", new File(dir).getParent()); fileList.add(0, map); } FileBrowserAdapter phoneFileBrowserAdapter = new FileBrowserAdapter( getApplicationContext(), fileList); fileListView.setAdapter(phoneFileBrowserAdapter);
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。
更多Android编写文件浏览器简单实现相关文章请关注PHP中文网!