学习android才一个星期,在一些资料上看见了一些例子,其中文件资源管理器的实现中科学的东西相当多.

在主页面FileManagerActivity中的代码:

package com.hoperun.activity; import java.io.File; import java.util.ArrayList; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.content.Intent; import android.content.DialogInterface.OnClickListener; import android.net.Uri; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import com.hoperun.adapter.FileListAdapter; public class FileManagerActivity extends ListActivity { /** Called when the activity is first created. */ private TextView showXPath;// 显示文件文件路径 private ArrayList<String> items;// 要显示的文件名 private ArrayList<String> paths;// 显示文件路径 private String rootPath = "/";// 根目录 private View renameDialogView;// 重命名对话框视图 private EditText nameEdit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); showXPath = (TextView) findViewById(R.id.xPath); getFileDir(rootPath);// 获取文件列表 } // 获取文件列表方法 private void getFileDir(String path) { showXPath.setText(path);//显示当前路径 items = new ArrayList<String>(); paths = new ArrayList<String>(); // 获取当前路径下的文件 File presentFile = new File(path); File[] files = presentFile.listFiles(); if (! path.equals(rootPath)) { // 返回根目录 items.add("back to /"); paths.add(rootPath); // 返回上一级目录 items.add("back previous"); paths.add(presentFile.getParent()); } // 添加当前路径下的所有的文件名和路径 for (File f : files) { items.add(f.getName()); paths.add(f.getPath()); } // 设置列表适配器 setListAdapter(new FileListAdapter(FileManagerActivity.this, items, paths)); } // List中item的点击事件 @Override protected void onListItemClick(ListView l, View v, int position, long id) { File f = new File(paths.get(position)); if (f.isDirectory()) { getFileDir(paths.get(position)); } else { fileHandle(f); } } // File对象处理方法 private void fileHandle(final File f) { // 设置监听器操作,在单击列表文件item的时候弹出对话框 OnClickListener clickListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == 0) {// 打开文件 openFile(f); } else if (which == 1) {// 修改文件名 // 创建修改文件名对话框 LayoutInflater renameInflater = LayoutInflater .from(FileManagerActivity.this); renameDialogView = renameInflater.inflate( R.layout.rename_alert_dialog, null); nameEdit = (EditText) renameDialogView.findViewById(R.id.nameEdit); // 设置编辑框内容未文件名 nameEdit.setText(f.getName()); // 在选项对话框中选择要进行的操作,在弹出的重命名对话框中的监听器 OnClickListener renameDialogListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String nameEditStr = nameEdit.getText().toString();// 获取重命名编辑框中的文本内容 final String postRenamedFilePath = f.getParentFile().getPath() + "/" + nameEditStr;// 重命名后的文件路径 if (new File(postRenamedFilePath).exists()) {// 修改后的文件名与已有的文件名冲突 // 不包括未修改文件名,直接点击确定的情况 if (! nameEditStr.equals(f.getName())) { new AlertDialog.Builder(FileManagerActivity.this) .setTitle("提示").setMessage("文件已经存在,是否覆盖?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { // 确定覆盖原有文件进行的操作 public void onClick(DialogInterface dialog, int which) { f.renameTo(new File(postRenamedFilePath));// 重命名覆盖文件 getFileDir(f.getParent());// 列出重命名后结果 } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int which) { } }).show(); } } else { f.renameTo(new File(postRenamedFilePath));// 重命名覆盖文件 getFileDir(f.getParent());// 列出重命名后结果 } } }; // 重命名对话框 AlertDialog renameDialog = new AlertDialog.Builder(FileManagerActivity.this).create(); renameDialog.setView(renameDialogView);// 设置重命名对话框的试图 renameDialog.setButton("确定", renameDialogListener); renameDialog.setButton2("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); renameDialog.show(); } else { // 删除选中的文件选项 new AlertDialog.Builder(FileManagerActivity.this).setTitle("删除") .setMessage("确定要删除此文件吗?").setPositiveButton("确定", new DialogInterface.OnClickListener() { // 确定删除文件 public void onClick(DialogInterface dialog, int which) { f.delete();// 删除文件 getFileDir(f.getParent()); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).show(); } } }; // 创建listDialog,选项对话框 String[] operas = new String[] {"open", "rename", "delete"}; new AlertDialog.Builder(FileManagerActivity.this) .setTitle("operator dialog").setItems(operas, clickListener) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).show(); } // 文件打开方法 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); } // 获取MIME Type类型 private String getMIMEType(File f) { String type = ""; String fileName = f.getName(); String end = fileName.substring(fileName.indexOf(".") + 1).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="*"; } // MIME Type格式是"文件类型/文件扩展名" type += "/*"; return type; } }

在主页面的xml中填写代码:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/white" > <TextView android:id="@+id/xPath" 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" /> </LinearLayout>

另外又有在文件列表中的一栏中要显示图标和文件名,需要两外加你了一个布局文件和自定义一个adapter:

在新建的布局文件中的代码:

<?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/imageIcon" android:layout_width="30dip" android:layout_height="30dip"> </ImageView> <TextView android:id="@+id/fileNameText" android:layout_gravity="center_vertical" android:layout_width="0dip" android:layout_weight="1.0" android:layout_height="wrap_content" android:textColor="@drawable/black" /> </LinearLayout>

在自定义的adapter中的代码:

package com.hoperun.adapter; import java.io.File; import java.util.List; import com.hoperun.activity.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 FileListAdapter extends BaseAdapter { private List<String> items; private List<String> paths; private LayoutInflater inflater; private Bitmap rootIcon; private Bitmap prevIcon; private Bitmap docIcon; private Bitmap folderIcon; public FileListAdapter() {} public FileListAdapter(Context context, List<String> items, List<String> paths) { // 从context获取一个布局加载器 inflater = LayoutInflater.from(context); this.items = items; this.paths = paths; // 初始化关联图标 rootIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.back_root); prevIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.back_prev); docIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.doc); folderIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.folder); } // 返回数据的记录条数 public int getCount() { return items.size(); } // 返回列表中的数据 public Object getItem(int position) { return items.get(position); } // 返回item的id值 public long getItemId(int position) { return position; } // 获取ListView中的item组件容器 public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null ; if (convertView == null) { convertView = inflater.inflate(R.layout.list_row, null); // 创建子组件容器 holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.fileNameText); holder.imageIcon = (ImageView) convertView.findViewById(R.id.imageIcon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // 根据文件不同类型,显示不同图标及文件名称 if (items.get(position).equals("back to /")) { holder.imageIcon.setImageBitmap(rootIcon); } else if (items.get(position).equals("back previous")) { holder.imageIcon.setImageBitmap(prevIcon); } else { File f = new File(paths.get(position)); if (f.isDirectory()) { holder.imageIcon.setImageBitmap(folderIcon); } else { holder.imageIcon.setImageBitmap(docIcon); } } holder.text.setText(items.get(position)); return convertView; } // 内部类,容器类 private class ViewHolder { TextView text; ImageView imageIcon; } }

还需建立重命名文件对话框AlertDialog相关的布局文件内容:

<?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="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/mText" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:text="@string/str_text" android:gravity="left" /> <EditText android:id="@+id/nameEdit" android:layout_height="wrap_content" android:layout_width="200px" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:autoText="false" android:gravity="fill_horizontal" /> </LinearLayout>

这样可以在最后运行,查看运行效果了...

更多相关文章

  1. android 反编译工具
  2. [JAVA] Android用到的一些文件操作
  3. android AppWidget的使用以及利用Service TimerTask实现widget的
  4. Android(安卓)Spinner 文字居中、其下拉窗口文字居中,自定义Spinn
  5. Android(安卓)遍历文件夹,搜索指定扩展名的文件
  6. 关于Android(安卓)LCD和键盘背光亮度 .
  7. android添加新驱动
  8. Android(安卓)Studio中AspectJ的简单使用一(自定义PointCut)
  9. 改变Android(安卓)对话框位置及边框

随机推荐

  1. 中介效应最新进展: 中介效应中的工具变量
  2. 以太网的下一阶段:400GE网络
  3. 微博基于Flink的机器学习实践
  4. 广义三阶段空间最小二乘估计法(GS3SLS)是
  5. 多业务融合推荐策略实践与思考
  6. 电商知识图谱
  7. FPGA设计笔记:QSPI Flash与DDR3L SDRAM采
  8. 空间计量各种模型代码笔记分享, 可以直接
  9. 从概念到应用,终于有人把数据挖掘讲明白了
  10. NBER最大规模改版! 超强大的搜索功能和人