Android 編寫文件瀏覽器

有時候咱們保存文件總會用到文件瀏覽器功能.那麼今天博主帶你們作一個。java


那麼開始,瀏覽文件,咱們就須要寫一個文件工具類。android

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);

具體的你們能夠看下源代碼。

下載地址:ide


效果圖工具

相關文章
相關標籤/搜索