1 /**根据给定的一个文件夹路径字符串遍历出这个文 2 * 件夹中包含的文件名称并配置到ListView列表中*/ 3 private void initFileListInfo(String filePath){ 4 isAddBackUp = false; 5 mCurrentFilePath = filePath; 6 //显示当前的路径 7 mPath.setText(filePath); 8 mFileName = new ArrayList<String>(); 9 mFilePaths = new ArrayList<String>(); 10 File mFile = new File(filePath); 11 //遍历出该文件夹路径下的所有文件/文件夹 12 File[] mFiles = mFile.listFiles(); 13 //只要当前路径不是手机根目录或者是sd卡根目录则显示“返回根目录”和“返回上一级” 14 if(menuPosition == 1&&!mCurrentFilePath.equals(mRootPath)){ 15 initAddBackUp(filePath,mRootPath); 16 }else if(menuPosition == 2&&!mCurrentFilePath.equals(mSDCard)){ 17 initAddBackUp(filePath,mSDCard); 18 } 19 20 /*将所有文件信息添加到集合中*/ 21 for(File mCurrentFile:mFiles){ 22 mFileName.add(mCurrentFile.getName()); 23 mFilePaths.add(mCurrentFile.getPath()); 24 } 25 26 /*适配数据*/ setListAdapter设置listView的适配器,要集成自ListActivity 27 setListAdapter(new FileAdapter(MainActivity.this,mFileName,mFilePaths)); 28 }
接着是列表的适配器(继承自BaseAdapter),要复写getCount()、getItem()、getItemId()以及getView()函数。
//自定义Adapter内部类 class FileAdapter extends BaseAdapter{ //返回键,各种格式的文件的图标 private Bitmap mBackRoot; private Bitmap mBackUp; private Bitmap mImage; private Bitmap mAudio; private Bitmap mRar; private Bitmap mVideo; private Bitmap mFolder; private Bitmap mApk; private Bitmap mOthers; private Bitmap mTxt; private Bitmap mWeb; private Context mContext; //文件名列表 private List<String> mFileNameList; //文件对应的路径列表 private List<String> mFilePathList; public FileAdapter(Context context,List<String> fileName,List<String> filePath){ mContext = context; mFileNameList = fileName; mFilePathList = filePath; //初始化图片资源 //返回到根目录 mBackRoot = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.back_to_root); //返回到上一级目录 mBackUp = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.back_to_up); //图片文件对应的icon mImage = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.image); //音频文件对应的icon mAudio = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.audio); //视频文件对应的icon mVideo = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.video); //可执行文件对应的icon mApk = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.apk); //文本文档对应的icon mTxt = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.txt); //其他类型文件对应的icon mOthers = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.others); //文件夹对应的icon mFolder = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.folder); //zip文件对应的icon mRar = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.zip_icon); //网页文件对应的icon mWeb = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.web_browser); } //获得文件的总数 public int getCount() { return mFilePathList.size(); } //获得当前位置对应的文件名 public Object getItem(int position) { return mFileNameList.get(position); } //获得当前的位置 public long getItemId(int position) { return position; } //获得视图 public View getView(int position, View convertView, ViewGroup viewgroup) { ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ViewHolder(); LayoutInflater mLI = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //初始化列表元素界面 convertView = mLI.inflate(R.layout.list_child, null); //获取列表布局界面元素 viewHolder.mIV = (ImageView)convertView.findViewById(R.id.image_list_childs); viewHolder.mTV = (TextView)convertView.findViewById(R.id.text_list_childs); //将每一行的元素集合设置成标签 convertView.setTag(viewHolder); } else { //获取视图标签 viewHolder = (ViewHolder) convertView.getTag(); } File mFile = new File(mFilePathList.get(position).toString()); //如果 if(mFileNameList.get(position).toString().equals("BacktoRoot")){ //添加返回根目录的按钮 viewHolder.mIV.setImageBitmap(mBackRoot); viewHolder.mTV.setText("返回根目录"); }else if(mFileNameList.get(position).toString().equals("BacktoUp")){ //添加返回上一级菜单的按钮 viewHolder.mIV.setImageBitmap(mBackUp); viewHolder.mTV.setText("返回上一级"); }else if(mFileNameList.get(position).toString().equals("BacktoSearchBefore")){ //添加返回搜索之前目录的按钮 viewHolder.mIV.setImageBitmap(mBackRoot); viewHolder.mTV.setText("返回搜索之前目录"); }else{ String fileName = mFile.getName(); viewHolder.mTV.setText(fileName); if(mFile.isDirectory()){ viewHolder.mIV.setImageBitmap(mFolder); }else{ String fileEnds = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length()).toLowerCase();//取出文件后缀名并转成小写 if(fileEnds.equals("m4a")||fileEnds.equals("mp3")||fileEnds.equals("mid")||fileEnds.equals("xmf")||fileEnds.equals("ogg")||fileEnds.equals("wav")){ viewHolder.mIV.setImageBitmap(mVideo); }else if(fileEnds.equals("3gp")||fileEnds.equals("mp4")){ viewHolder.mIV.setImageBitmap(mAudio); }else if(fileEnds.equals("jpg")||fileEnds.equals("gif")||fileEnds.equals("png")||fileEnds.equals("jpeg")||fileEnds.equals("bmp")){ viewHolder.mIV.setImageBitmap(mImage); }else if(fileEnds.equals("apk")){ viewHolder.mIV.setImageBitmap(mApk); }else if(fileEnds.equals("txt")){ viewHolder.mIV.setImageBitmap(mTxt); }else if(fileEnds.equals("zip")||fileEnds.equals("rar")){ viewHolder.mIV.setImageBitmap(mRar); }else if(fileEnds.equals("html")||fileEnds.equals("htm")||fileEnds.equals("mht")){ viewHolder.mIV.setImageBitmap(mWeb); }else { viewHolder.mIV.setImageBitmap(mOthers); } } } return convertView; } //用于存储列表每一行元素的图片和文本 class ViewHolder { ImageView mIV; TextView mTV; } }
文件(夹)的复制(用FileInputStream的read()方法逐个字节读入,同时用FileOutputStream逐个字节写入文件)。
private int i; FileInputStream fis; FileOutputStream fos; //复制文件 private void copyFile(String oldFile,String newFile){ try { fis = new FileInputStream(oldFile); fos = new FileOutputStream(newFile); do{ //逐个byte读取文件,并写入另一个文件中 if((i = fis.read()) != -1){ fos.write(i); } }while(i != -1); //关闭输入文件流 if(fis != null){ fis.close(); } //关闭输出文件流 if(fos != null){ fos.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }