Android 文件浏览器总结

文件浏览器用来读取android系统中的所有文件和文件夹。具体说明如下:

  1. 最上面显示当前的文件路径。如果是根目录,则显示“根目录”;
  2. 第二行是返回上一级按钮。如果当前处于根目录下,则该行不显示;
  3. 文件显示使用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>
时间: 2024-08-02 18:03:47

Android 文件浏览器总结的相关文章

发现一个好用的Android文件访问工具--es文件管理器,可以在同局域网下的浏览器中查看手机中的文件

如题,发现一个好用的Android文件访问工具--es文件管理器,可以在同局域网下的浏览器中查看手机中的文件 1.在手机上打开es文件管理器的远程管理器,如图:                   2.保证手机和电脑在同一个局域网下(有线或者Wifi都可以) 贴一下我的地址: (1)我电脑的IP地址(有线宽带连接): (2)手机的地址(Wifi): 3.在别的手机或者电脑上打开上面的Url(我的是:ftp://192.168.1.115:3721/),打开后就是这样的: 3. 看一下刚才在手机上

Android简单文件浏览器源代码 (转)

Android简单文件浏览器源代码 (转) activity_main .xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height=&qu

Android入门之文件系统操作(一)简单的文件浏览器 (转)

Android入门之文件系统操作(一)简单的文件浏览器 (转)        1. import java.io.File; 2. import java.util.*; 3. 4. import android.app.Activity; 5. import android.content.Context; 6. import android.os.*; 7. import android.view.*; 8. import android.widget.*; 9. import androi

Android简单的文件浏览器,ListActivity的简单用法

2014-07-29 13:39:09MainActivity.java package com.example.sample_4_21; import java.io.File; import java.util.ArrayList; import java.util.List; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; imp

Android应用源码可以按音乐视频图片分类浏览的安卓文件浏览器

本项目是一个可按音乐.图片.视频分类浏览的安卓文件浏览器.具有多选.打开.复制.粘贴.删除.重命名.查看属性等功能,并且还可以切换列表显示效果和对文件分类浏览,比较遗憾的是源码注释不多,项目编码UTF-8编译版本2.3.3.MediaCenter是程序的主项目依赖moduleAboutus和moduleAnimtab两个项目,三个项目如果导入以后没有自动添加引用关系需要手动在Library里面添加引用关系. 代码截图: 运行效果截图: 源码包免费下载地址:[点击这里]

简单文件浏览器--实现简易文件选取返回路径功能

因为今天学习Andorid Sample一个示例的时候需要使用一个选取图片的功能,示例中给的Intent没有办法用,因此,自己搜集资料来实现这个简单的文件浏览器,主要这两个文件就贴在下面了: 首先是模型层,定义显示的文件实体(保存文件对应的Icon和其简易名称与绝对路径) 1 public class DirEntry { 2 private int icon; 3 private String dirName; 4 private String path; 5 6 public String

关于Android文件Apk下载的那点事

1.Android文件Apk下载变ZIP压缩包解决方案 如果你的下载服务器为Nginx服务器,那么,在Nginx安装目录下的conf/mime.types文件的对应位置,加上以下一行语句,指定APK文件的MIME类型为 application/vnd.android.package-archive 即可: [html] view plaincopy application/vnd.android.package-archive     apk; 如果是java-web服务器 只需要修改web.x

一步一步打造自己的Android图片浏览器(原创)

今天我们试着来制作一个自己的Android图片浏览器. 图片浏览器应该具有什么功能呢?鉴于不同的人不同的理解,这里提出一个基本的需求: 搜索手机内的所有图片,展示于一个列表中: 列表中展示的是图片的缩略图,点击图片之后,进入图片的大图显示: 在大图显示状态下,可以进行左右滑动,查看其它图片: 在大图显示状态下,我们应该可以查看图片的详细信息: 也许我们还可以支持大图下的放大与缩小? 好了,要求先这么多,我们来实现吧. 第一步:我们要得到手机内的所有图片,展示在一个列表中. android内部为我

安卓分类文件浏览器应用源码

安卓分类文件浏览器应用源码,本项目是一个可按音乐.图片.视频分类浏览的安卓文件浏览器.具有多选.打开.复制.粘贴.删除.重命名.查看属性等功能,并且还可以切换列表显示效果和对文件分类浏览,比较遗憾的是源码注释不多,项目编码UTF-8编译版本2.3.3.MediaCenter是程序的主项目依赖moduleAboutus和moduleAnimtab两个项目,三个项目如果导入以后没有自动添加引用关系需要手动在Library里面添加引用关系. <ignore_js_op> 文件浏览 <ignor