zoukankan      html  css  js  c++  java
  • Android 编程之入门开发目录管理器开发文件事件操作-2

    上一篇博客,我们已经得到了目录列表,我们须要对文件列表子项加入事件,比方我们点击的是文件。就运行

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

    与目录的处理后面会讲到


    在我的程序里。我写了一个类。对文件进行处理。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,右上角小+号消失。生成删除button和复制button,点击Item选中,更改选中Item项背景颜色:




    运行批量粘贴,这里我仅仅点了5项。所以仅仅粘贴了5个目录。大家注意看右上角图标,又恢复到了没有复制操作的时候的图标,事实上在点击复制button之后,会加入一个粘贴button,粘贴完后消失:



    批量删除操作:



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



  • 相关阅读:
    React开发流程及认识
    Python整合AnaConda
    计算机显示器扩展方向调整
    python3 urlencode 和 urldecode 使用
    解决开启Vue项目缺少node_models包问题
    解决开启Vue项目缺少node_models包问题
    python中列表相加
    python炒鸡面试题
    Django中的CBV控制前后端分离模式
    selenuim使用总结
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6855264.html
Copyright © 2011-2022 走看看