首先看效果图
第一个Activity很简单就是一个按钮Button 加一个 TextView
见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"
- >
- <Button
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="选择文件"
- />
- <TextView
- android:id="@+id/fileText"
- android:gravity="center"
- android:textSize="20px"
- android:textColor="#219ac6"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </LinearLayout>
Button弹出显示文件夹选择框
TextView就是用来显示当前选择的文件夹路径
- public class MainAcivity extends Activity{
-
- public static final int FILE_RESULT_CODE = 1;
-
- private TextView textView;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button button = (Button)findViewById(R.id.button);
- textView = (TextView)findViewById(R.id.fileText);
- button.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
- Intent intent = new Intent(MainAcivity.this,MyFileManager.class);
- startActivityForResult(intent, FILE_RESULT_CODE);
- }
- });
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if(FILE_RESULT_CODE == requestCode){
- Bundle bundle = null;
- if(data!=null&&(bundle=data.getExtras())!=null){
- textView.setText("选择文件夹为:"+bundle.getString("file"));
- }
- }
- }
- }
这里通过startActivityForResult()弹出的Activity 所以要重写onActivityResult方法
在目标Activity调用方法finish()的时候会回调
这里需要注意在按返回键时也会调用该方法 所以这里还需要判空
然后再 MyFileManager 这个Activity中通过ListView来显示文件列表
布局文件 fileselect.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="250px"
- android:layout_height="400px"
- 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="fill_parent"
- android:layout_height="330px"
- >
- </ListView>
- <LinearLayout
- android:gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:background="@drawable/white"
- >
- <Button
- android:id="@+id/buttonConfirm"
- android:layout_width="125px"
- android:layout_height="fill_parent"
- android:text="确定"
- />
- <Button
- android:id="@+id/buttonCancle"
- android:layout_width="125px"
- android:layout_height="fill_parent"
- android:text="取消"
- />
-
- </LinearLayout>
- </LinearLayout>
列表显示Item 通过布局文件 file_row.xml
- <?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"
- android:background="#ffffff"
- >
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/listview_selected"
- android:padding="6px"
- >
- <ImageView android:id="@+id/icon"
- android:layout_width="30dip"
- android:layout_height="30dip"
- >
- </ImageView>
- <TextView android:id="@+id/text"
- android:layout_gravity="center_horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textColor="@drawable/black"
- >
- </TextView>
- </LinearLayout>
- </LinearLayout>
这里简单实现点击变换底色的效果 通过两层LinearLayout 的方式实现 创建一个select可以自定义点击效果 就不赘述了
文件选择代码
- package com.debby;
- 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.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.TextView;
- public class MyFileManager extends ListActivity {
- private List<String> items = null;
- private List<String> paths = null;
- private String rootPath = "/";
- private String curPath = "/";
- private TextView mPath;
- private final static String TAG = "bb";
- @Override
- protected void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.fileselect);
- mPath = (TextView) findViewById(R.id.mPath);
- Button buttonConfirm = (Button) findViewById(R.id.buttonConfirm);
- buttonConfirm.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent data = new Intent(MyFileManager.this, MainAcivity.class);
- Bundle bundle = new Bundle();
- bundle.putString("file", curPath);
- data.putExtras(bundle);
- setResult(2, data);
- finish();
- }
- });
- Button buttonCancle = (Button) findViewById(R.id.buttonCancle);
- buttonCancle.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- finish();
- }
- });
- 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());
- }
- for (int i = 0; i < files.length; i++) {
- File file = files[i];
- items.add(file.getName());
- paths.add(file.getPath());
- }
- setListAdapter(new MyAdapter(this, items, paths));
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- File file = new File(paths.get(position));
- if (file.isDirectory()) {
- curPath = paths.get(position);
- getFileDir(paths.get(position));
- } else {
-
- }
- }
- }
- import java.io.File;
- import java.util.List;
- import com.debby.R;
- 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;
- public class MyAdapter extends BaseAdapter
- {
- private LayoutInflater mInflater;
- private Bitmap mIcon1;
- private Bitmap mIcon2;
- private Bitmap mIcon3;
- private Bitmap mIcon4;
- private List<String> items;
- private List<String> paths;
- 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)
- {
- convertView = mInflater.inflate(R.layout.file_row, null);
- 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());
- if(items.get(position).toString().equals("b1"))
- {
- holder.text.setText("返回根目录..");
- holder.icon.setImageBitmap(mIcon1);
- }
- else if(items.get(position).toString().equals("b2"))
- {
- holder.text.setText("返回上一层..");
- holder.icon.setImageBitmap(mIcon2);
- }
- else
- {
- holder.text.setText(f.getName());
- if(f.isDirectory())
- {
- holder.icon.setImageBitmap(mIcon3);
- }
- else
- {
- holder.icon.setImageBitmap(mIcon4);
- }
- }
- return convertView;
- }
- private class ViewHolder
- {
- TextView text;
- ImageView icon;
- }
- }
在 onListItemClick 事件中改变ListView的Adapter实现打开文件夹
这里点击文件夹时可以打开目录,如果选择文件可以根据类型来启用不同工具来打开
方法如下
- private void openFile(File f) {
- Intent intent = new Intent();
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.setAction(android.content.Intent.ACTION_VIEW);
- String type = getMIMEType(f);
- intent.setDataAndType(Uri.fromFile(f), type);
- startActivity(intent);
- }
- private String getMIMEType(File f) {
- String type = "";
- String fName = f.getName();
- String end = fName
- .substring(fName.lastIndexOf(".") + 1, fName.length())
- .toLowerCase();
- 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;
- }
根据文件的后缀名来确定type 然后打开文件,打开方式通过setDataAndType方法设置