可是实现点击选择文件即可到手机目录下选择文件。
代码下载路径:http://download.csdn.net/detail/u011324501/9529924
代码实现:MainActivity.java
package com.debby; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; 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")); } } } }2、MyFileManager.java
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 = "MyFileManager"; @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 { 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); 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 += "/*"; Log.d(TAG, "type:"+type); return type; } }3、MyAdapter.java
package com.debby; 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); } 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) { 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; } }布局代码: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>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>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>