Android实现文件资源管理器雏形

Flower ·
更新时间:2024-09-20
· 1416 次阅读

本文实例为大家分享了Android文件资源管理器雏形的实现代码,供大家参考,具体内容如下

学习Android,应该在掌握单个知识点之后,把多个知识点串联起来实现一些有一定代码量的小项目比较好。当然,这也是我教学中总结出来的一点经验心得,并不适合所有人。

先做需求分析(实现的功能):

1.ListView开始显示sdcard目录下的子目录和文件。

2.点击文件,Toast显示“点击的是文件”

3.点击目录,进入子目录,显示子目录下的子目录和文件。

4.back键回退到上层目录。

5.异常情况处理:

5.1如果sdcard没有插入,则不显示列表,且提示用户应该插入sdcard后操作

5.2不允许进入sdcard的上层目录

下面开始实现:

布局有两个:

1.主布局:file_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".FileExplorerActivity" >     <TextView          android:id="@+id/currentTv"         android:layout_alignParentTop="true"         android:clickable="true"         android:layout_width="match_parent"         android:layout_height="wrap_content"/>     <ListView          android:id="@+id/fileLv"         android:layout_below="@id/currentTv"         android:layout_alignParentBottom="true"         android:layout_width="match_parent"         android:layout_height="wrap_content">     </ListView> </RelativeLayout>

布局很简单,就是放置了一个ListView控件,这里要注意的是,ListView标签下不能再放入其他的子控件。内容是通过子布局和Adapter来显示的。

2.ListView中的子布局file_list_item.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="horizontal" >     <ImageView          android:id="@+id/icon"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         />     <TextView         android:id="@+id/filename"         android:layout_width="wrap_content"         android:layout_height="wrap_content"/> </LinearLayout>

子布局也很简单,就是在水平方向上左边显示一个图标,用来显示文件夹或文件图标,右边显示文件名。

3.Activity代码(功能点写在注释中)

public class FileExplorerActivity extends Activity {     //Adapter中ICON和Filename键值对常量     private static final String ICON = "icon";     private static final String FILENAME = "filename";     private TextView currentTv;//ListView上显示当前路径的TextView     private ListView fileLv;//文件列表显示的ListView     SimpleAdapter adapter;//适配器     private List<HashMap<String, Object>> data;//填充的数据     private File root;//文件夹根节点     private File[] currentFiles; //根节点下的所有文件(包括文件夹)     private File currentPath;//记录当前节点     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_file_explorer);         currentTv = (TextView)findViewById(R.id.currentTv);         fileLv = (ListView)findViewById(R.id.fileLv);         //得到根节点root -->/mnt/sdcard         root = getFileSystemRoot();         //得到第一屏的信息         if(root != null){             //从/mnt/sdcard下得到文件列表             data = getFileListFromSdcard(root);         }else{             //如果没有挂载sdcard,则提示用户             data = new ArrayList<HashMap<String, Object>>();             HashMap<String, Object> map = new HashMap<String, Object>();             map.put(ICON, R.drawable.none);             map.put(FILENAME, "逗我玩啊,插卡啊");             data.add(map);         }         //创建Adapater         adapter = new SimpleAdapter(                     this,                      data,                      R.layout.file_list_item,                      new String[]{ICON, FILENAME},                      new int[]{R.id.icon, R.id.filename});         fileLv.setAdapter(adapter);         //绑定事件         fileLv.setOnItemClickListener(new OnItemClickListener() {             @Override             public void onItemClick(AdapterView<?> parent, View view,                     int position, long id) {                 //点击listview 项时,如果是目录,则进入下一层次,如果是文件,不做处理                 File currentPosFile = currentFiles[position];                 if(currentPosFile.isDirectory()){                     getFileListFromSdcard(currentPosFile);                 }else{                     Toast.makeText(FileExplorerActivity.this, "您点击的是文件夹", Toast.LENGTH_LONG).show();                 }             }         });     }     /**      * 拦截back键返回      * @return      */     @Override     public boolean onKeyDown(int keyCode, KeyEvent event) {         if(KeyEvent.KEYCODE_BACK == keyCode){             File parentFile = currentPath.getParentFile();             //不能超过最顶层             try {                 if(parentFile.getCanonicalPath().toString().equals("/mnt")){                     Toast.makeText(this, "别按了,到家了", Toast.LENGTH_LONG).show();                     return true;                 }else{                     getFileListFromSdcard(parentFile);                 }             } catch (IOException e) {                 e.printStackTrace();             }         }         return super.onKeyDown(keyCode, event);     }     private File getFileSystemRoot() {         //首先得到Sd卡是否加载了         if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){             //得到sd卡路径 root --> /mnt/sdcard             root = Environment.getExternalStorageDirectory();         }else{             Toast.makeText(this, "逗我玩啊,插卡啊", Toast.LENGTH_LONG).show();         }         return root;     }     /**      * 得到Sdcard中的文件列表      * @return      */     private List<HashMap<String, Object>> getFileListFromSdcard(File root) {         try {             currentPath = root;             currentTv.setText(root.getCanonicalPath().toString());         } catch (IOException e) {             e.printStackTrace();         }         List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();         currentFiles = root.listFiles();//列出当前目录下的所有文件和目录         for(File f : currentFiles){             HashMap<String, Object> map = new HashMap<String, Object>();             String fileName = f.getName();             int icon;             if(f.isDirectory()){                 icon = R.drawable.dir;                 map.put(ICON, icon);             }else if(f.isFile()){                 icon = R.drawable.file;                 map.put(ICON, icon);             }             map.put(FILENAME, fileName);             list.add(map);         }         //把原来的data list清空,然后把list放进去,再通知adapter         if(data != null){             data.clear();             data.addAll(list);             adapter.notifyDataSetChanged();         }         return list;     } }

运行效果:

功能展望:

以上代码是通过精简功能达到的,如果要增加以下功能也是相当之简单的:

1.文件夹和文件的删除功能

2.文件夹和文件的重命名功能

3.文件的分类调用App查看功能

4.文件详细信息显示功能

...

从上面示例可以看出,其实做一个文件资源管理器是相当简单的。



Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号