文件列表浏览的Demo

1.main.java文件:
Java代码
package com.test;

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;

protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
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());
}
if (files != null) {
/* 将所有文件添加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被点击时要做的动作 */
protected void onListItemClick(ListView l, View v, int position, long 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;
}
}

package com.test;

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;

protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
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());
}
if (files != null) {
/* 将所有文件添加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被点击时要做的动作 */
protected void onListItemClick(ListView l, View v, int position, long 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;
}
}


2. MyAdapter.java适配器类:
Java代码

package com.test;

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:文件的图档
*/
private LayoutInflater mInflater;
private Bitmap mIcon1;
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);
}

/* 因继承BaseAdapter,需覆盖以下方法 */
public int getCount() {
return items.size();
}

public Object getItem(int position) {
return items.get(position);
}

public long getItemId(int position) {
return position;
}

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);
} 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);
}
/* 设置[回到上一层]的文字与icon */
else if (items.get(position).toString().equals("b2")) {
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;
}
}


package com.test;

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:文件的图档
*/
private LayoutInflater mInflater;
private Bitmap mIcon1;
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);
}

/* 因继承BaseAdapter,需覆盖以下方法 */
public int getCount() {
return items.size();
}

public Object getItem(int position) {
return items.get(position);
}

public long getItemId(int position) {
return position;
}

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);
} 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);
}
/* 设置[回到上一层]的文字与icon */
else if (items.get(position).toString().equals("b2")) {
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;
}
}


3.main.xml文件很简单:
Java代码
<?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">
</TextView>
<ListView android:id="@android:id/list" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>

<?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">
</TextView>
<ListView android:id="@android:id/list" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>


4.显示item的xml:
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/icon" android:layout_width="30dip"
android:layout_height="30dip">
</ImageView>
<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">
</TextView>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值