MyAdapter.java
package src.com;
import java.io.File;
import java.util.List;import android.content.Context;
import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;/*自定义的Adapter,继承android.widget.BaseAdapter*/
public class MyAdapter extends BaseAdapter { /* * 声明变量 * mIcon1:回到根目录的图文件 * mIcon2:回到上一层的图档 * mIcon3:文件夹的图文件 * mIcon4:文件的图档 */ //Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。 //LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。 private LayoutInflater mInflater; private Bitmap mIcon1;//Bitmap位图 private Bitmap mIcon2; private Bitmap mIcon3; private Bitmap mIcon4; private List<String> items; private List<String> paths; /* MyAdapter的构造器,传入三个参数 */ public MyAdapter(Context context, List<String> it, List<String> pa){ /* 参数初始化 */ mInflater = LayoutInflater.from(context); items = it; paths = pa; mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.back01); mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.back02); mIcon3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.folder); mIcon4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.doc); }@Override
public int getCount() { return items.size(); }@Override
public Object getItem(int position) { return items.get(position); }@Override
public long getItemId(int position) { return position; }@Override
public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if( convertView == null ){ /* 使用自定义的file_row作为Layout */ convertView = mInflater.inflate(R.layout.file_row, null); /* 初始化holder的text与icon */ holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.text); holder.icon = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder);//Adapter 有个getView方法,可以使用setTag把查找的view缓存起来方便多次重用 }else{ holder = (ViewHolder) convertView.getTag(); } File f = new File(paths.get(position).toString()); /* 设置[回到根目录]的文字与icon */ if(items.get(position).toString().equals("b1")){ holder.text.setText("Back to /"); holder.icon.setImageBitmap(mIcon1); }else if(items.get(position).toString().equals("b2")){/* 设置[回到上一层]的文字与icon */ holder.text.setText("Back to .."); holder.icon.setImageBitmap(mIcon2); }else{/* 设置[文件或文件夹]的文字与icon */ holder.text.setText(f.getName()); if(f.isDirectory()){ holder.icon.setImageBitmap(mIcon3); }else{ holder.icon.setImageBitmap(mIcon4); } } return convertView; } /* class ViewHolder */ private class ViewHolder{ TextView text; ImageView icon; }}
Main.java
package src.com;
import java.io.File;
import java.util.ArrayList;import java.util.List;import android.app.ListActivity;
import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.ListView;import android.widget.TextView;public class Main extends ListActivity {
/* * 变量声明 * items:存放显示的名称 * paths:存放文件路径 * rootPath:起始目录 */ private List<String> items = null; private List<String> paths = null; private String rootPath = "/"; private TextView mPath;@Override
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); mPath = (TextView) findViewById(R.id.mPath); getFileDir(rootPath); }private void getFileDir(String filePath) {
/* 设置目前所在路径 */ mPath.setText(filePath); items = new ArrayList<String>(); paths = new ArrayList<String>(); File f = new File(filePath); File[] files = f.listFiles(); if(!filePath.equals(rootPath)){ /* 第一笔设置为[回到根目录] */ items.add("b1"); paths.add(rootPath); /* 第二笔设置为[回到上一层] */ items.add("b2"); paths.add(f.getParent()); } /* 将所有文件添加ArrayList中 */ for(int i = 0; i < files.length; i++){ File file = files[i]; items.add(file.getName()); paths.add(file.getPath()); } /* 使用自定义的MyAdapter来将数据传入ListActivity */ setListAdapter(new MyAdapter(this,items,paths)); } /* 设置ListItem被点击时要做的动作 */ @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); File file = new File(paths.get(position)); if(file.isDirectory()){ /* 如果是文件夹就再运行getFileDir() */ getFileDir(paths.get(position)); }else{ /* 如果是文件就运行openFile() */ openFile(file); } } /* 在手机上打开文件的方法 */ private void openFile(File f){ Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//此操作放入到一个新的任务中 intent.setAction(android.content.Intent.ACTION_VIEW);//显示用户数据 /* 调用getMIMEType()来取得MimeType */ String type = getMIMEType(f); /* 设置intent的file与MimeType */ intent.setDataAndType(Uri.fromFile(f), type); startActivity(intent); } /* 判断文件MimeType的方法 */ private String getMIMEType(File f){ String type = ""; String fName = f.getName(); /* 取得扩展名 */ String end = fName.substring(fName.lastIndexOf(".")+1,fName.length()).toLowerCase(); /* 依附档名的类型决定MimeType */ if(end.equals("m4a")||end.equals("mp3")||end.equals("mid") ||end.equals("xmf")||end.equals("ogg")||end.equals("wav")){ type = "audio"; }else if(end.equals("3gp")||end.equals("mp4")){ type = "video"; }else if(end.equals("jpg")||end.equals("gif")||end.equals("png") ||end.equals("jpeg")||end.equals("bmp")){ type = "image"; }else{ /* 如果无法直接打开,就跳出软件列表给用户选择 */ type = "*"; } type += "/*"; return type; }}
layout目录下
file_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" >"<ImageView
android:id="@+id/icon" android:layout_width="30dip" android:layout_height="30dip" /> <TextView android:id="@+id/text" android:layout_gravity="center_vertical" android:layout_width="0dip" android:layout_weight="1.0" android:layout_height="wrap_content" android:textColor="@drawable/black" /></LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/white" ><TextView
android:id="@+id/mPath" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px" android:textSize="18sp" android:textColor="@drawable/blue" /> <ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView>"</LinearLayout>