Android 编程之入门开发文件夹管理器开发文件事件操作-2

来源:互联网 发布:伤感网络歌手 编辑:程序博客网 时间:2024/06/05 10:13

上一篇博客,我们已经得到了文件夹列表,我们需要对文件列表子项添加事件,比如我们点击的是文件,就执行

打开操作,点击的是文件夹执行打开文件夹操作,遍历文件清单,以此类推直到最后一个是文件位置,关于文件

与文件夹的处理后面会讲到


在我的程序里,我写了一个类,对文件进行处理,FileOpreationUitl:

package com.example.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.content.Context;import android.util.Log;import android.widget.Toast;/** * 文件的操作类 * @author Engineer-Jsp * @date 2014.10.27 */public class FileOpreationUitl {public static Map<String,Object> mp3_List=new HashMap<String,Object>();public static List<Map<String, Object>>mp3_data=new ArrayList<Map<String,Object>>();public static String mp3_listitem;//public static Map<String,Object> picture_List=new HashMap<String,Object>();public static List<Map<String, Object>>picture_data=new ArrayList<Map<String,Object>>();public static String picture_listitem;//public static Map<String,Object> video_List=new HashMap<String,Object>();public static List<Map<String, Object>> video_data=new ArrayList<Map<String,Object>>();public static String video_listitem;//删除文件和目录public void  deleteFile(File path){//如果传来的参数path是文件,直接执行删除操作if(path.isFile()){//删除path.delete();//否则为文件夹,执行下面的操作}else{//定义文件数组接收参数Path文件夹的文件列表File[] files=path.listFiles();//循环编历文件for(File  f : files){//如果是文件执行if(f.isFile()){//删除f.delete();}else{//调用自己递归deleteFile(f);}}//删除目录path.delete();}}//复制文件public void  copyFile(File currentpath,File srcpath){File newFile=new File(srcpath, currentpath.getName());if(!newFile.exists()){try {newFile.createNewFile();FileInputStream fileinput=new FileInputStream(currentpath);FileOutputStream fileout=new FileOutputStream(newFile);byte[] byt=new byte[1024 * 16];int length=0;while((length=fileinput.read(byt))!=-1){fileout.write(byt,0,length);}fileinput.close();fileout.close();} catch (IOException e) {e.printStackTrace();}}//else{//newFile.delete();//copyFile(currentpath, srcpath);//}}//复制文件夹public void  copyDirectory(File currentpath,File srcpath){if(currentpath.isFile()){copyFile(currentpath, srcpath);}else{File file=new File(srcpath, currentpath.getName());if(!file.exists()){file.mkdir();}//else{//file.delete();//}File[] files=currentpath.listFiles();for(File f : files){copyDirectory(f, file);}//删除目录//currentpath.delete();}}//新建public void  newFile(File currentpath){if(!currentpath.exists()){currentpath.mkdirs();}}//音乐分类/** *  * @param groupPath  如果你想获取SDcard下面的所以mp3文件你就填sdcard路径 * 用的是递归的方式获取 */public void getReciver(File mp3_Path){//循环获取sdcard目录下面的目录和文件for(int i=0; i< mp3_Path.listFiles().length; i++){File childFile = mp3_Path.listFiles()[i];//假如是目录的话就继续调用getSDcardFile()将childFile作为参数传递的方法里面if(childFile.isDirectory()){getReciver(childFile);}else{//如果是文件的话,判断是不是以.mp3结尾,是就加入到List里面if(childFile.toString().endsWith(".mp3")){mp3_List.put(mp3_listitem,childFile.getName().toString());mp3_data.add(mp3_List);//打印文件的文件名System.out.println(childFile.getName());Log.d("XXXXXXXXXX",childFile.getName());//打印文件的路径System.out.println(childFile.getAbsolutePath());Log.d("XXXXXXXXXX",childFile.getAbsolutePath());}}}}    //图片分类public void getPicture(File picture_Path){//循环获取sdcard目录下面的目录和文件for(int i=0; i<picture_Path.listFiles().length; i++){File childFile =picture_Path.listFiles()[i];//假如是目录的话就继续调用getSDcardFile()将childFile作为参数传递的方法里面if(childFile.isDirectory()){getPicture(childFile);}else{//如果是文件的话,判断是不是以.mp3结尾,是就加入到List里面if(childFile.toString().endsWith(".png")||childFile.toString().endsWith(".gif")||childFile.toString().endsWith(".bmp")||childFile.toString().endsWith(".jpg")){picture_List.put(picture_listitem,childFile.getName().toString());picture_data.add(picture_List);//打印文件的文件名System.out.println(childFile.getName());Log.d("XXXXXXXXXX",childFile.getName());//打印文件的路径System.out.println(childFile.getAbsolutePath());Log.d("XXXXXXXXXX",childFile.getAbsolutePath());}}}}//视频分类public void getVideo(File video_Path){//循环获取sdcard目录下面的目录和文件for(int i=0; i<video_Path.listFiles().length; i++){File childFile = video_Path.listFiles()[i];//假如是目录的话就继续调用getSDcardFile()将childFile作为参数传递的方法里面if(childFile.isDirectory()){getVideo(childFile);}else{//如果是文件的话,判断是不是以.mp3结尾,是就加入到List里面if(childFile.toString().endsWith(".mp4")||childFile.toString().endsWith(".avi")||childFile.toString().endsWith(".rmvb")||childFile.toString().endsWith(".3gp")){video_List.put(video_listitem,childFile.getName().toString());video_data.add(video_List);//打印文件的文件名System.out.println(childFile.getName());Log.d("XXXXXXXXXX",childFile.getName());//打印文件的路径System.out.println(childFile.getAbsolutePath());Log.d("XXXXXXXXXX",childFile.getAbsolutePath());}}}}//搜索//public void  searchFile(File path){////}}

配合 MultiChoiceModeListener 执行多选,优于 setChoiceMode 单选,让application可以执行批量的操作处理,包括复制、删除等,下面看看效果:

执行新建测试,点击右上角小+号:



点击确定,生成文件夹,刷新列表:



下面看看批量复制操作,长按ListView Item,右上角小+号消失,生成删除按钮和复制按钮,点击Item选中,更改选中Item项背景颜色:




执行批量粘贴,这里我只点了5项,所以只粘贴了5个文件夹,大家注意看右上角图标,又恢复到了没有复制操作的时候的图标,其实在点击复制按钮之后,会添加一个粘贴按钮,粘贴完后消失:



批量删除操作:



文件操作大概就写了这些,有需要的可以自己拓展,我这里主要是方便大伙学习,谢谢~



0 0
原创粉丝点击