团队博客9
Android文件获取
使用递归便利本地所有文件(txt文件、word文件、ppt文件和zip压缩文件 )
@SuppressLint("NewApi") public class FileInforService { public File path1; public ArrayList<FileInfor> listfile; public Context context; public ArrayList<FileInfor> lpath; public ArrayList<File> afile; public FileInforService(Context context) { this.context=context; } /*获取所有文件信息*/ public ArrayList<File> getAllFileList() { listfile=new ArrayList<FileInfor>(); afile=new ArrayList<File>(); File file=Environment.getExternalStorageDirectory(); File[] files=file.listFiles(); runFile(files); return afile; } public void runFile(File[] file) { if (file != null) { for (int i=0;i<file.length;i++) { File file2=file[i]; System.out.println(file2.toString()); if (file2.listFiles() == null) { System.out.println(file2.isFile()); if(file2.isFile()) { afile.add(file2); } } else { final File[] newFileList=file2.listFiles(); Thread thread=new Thread(){ @Override public void run() { // TODO Auto-generated method stub runFile(newFileList); } }; thread.start(); } } } } }
private ArrayList<FileInfor> getFileList() { ArrayList<File> files=new ArrayList<File>(); service=new FileInforService(inbox); files=service.getAllFileList(); int fsize=files.size(); if(fsize==0) { return null; } for(int i=0;i<fsize;i++) { File file=files.get(i); String name=file.getName(); int a=0; if(name.endsWith("xls")||name.endsWith("xlsx")||name.endsWith("xlsm")||name.endsWith("xltm")||name.endsWith("xlsb")||name.endsWith("xlam")) { /* Bitmap icon=BitmapFactory.decodeResource(getResources(), R.drawable.excel); */ a=1; } if(name.endsWith("ppt")||name.endsWith("pptx")||name.endsWith("pptm")||name.endsWith("ppsx")||name.endsWith("potx")||name.endsWith("potm")||name.endsWith("ppam")) { a=2; } if(name.endsWith("doc")||name.endsWith("docx")||name.endsWith("docm")||name.endsWith("dotx")||name.endsWith("dotm")) { a=3; } if(name.endsWith("umd")||name.endsWith("pdf")||name.endsWith("txt")||name.endsWith("edk")||name.endsWith("chm")) { a=4; } if(name.endsWith("zip")||name.endsWith("rar")||name.endsWith("iso")||name.endsWith("7z")) { a=5; } if(a==0) { continue; } Map<String,Object> map=new HashMap<String,Object>(); String str = "文件名称:" + file.getName() + " 路径:"+ file.getPath(); System.out.println(str); FileInfor fileInfor=new FileInfor(); String path=file.getAbsolutePath(); int size=(int)file.length(); String fileSize=parseApkSize(size); long modTime=file.lastModified(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date=dateFormat.format(new Date(modTime)); map.put("name", name); Toast.makeText(inbox, fileInfor.getName(),Toast.LENGTH_SHORT).show(); map.put("path", path); map.put("date",date); map.put("size", fileSize); if(a==1) { map.put("icon", R.drawable.excel); listExcel.add(map); } if(a==2) { map.put("icon", R.drawable.ppt); listPpt.add(map); } if(a==3) { map.put("icon", R.drawable.word); listWord.add(map); } if(a==4) { map.put("icon", R.drawable.pdf); listPdf.add(map); } if(a==5) { map.put("icon", R.drawable.zip); listZip.add(map); } } return null; } private String parseApkSize(int size) { BigDecimal bd; BigDecimal setScale; String str=null; if(size/1024>0) { bd=new BigDecimal((double)size/(1024)); setScale = bd.setScale(2, BigDecimal.ROUND_DOWN); str=setScale.toString()+"KB"; } else { bd = new BigDecimal((double)size/(1024*1024)); setScale = bd.setScale(2, BigDecimal.ROUND_DOWN); str=setScale.toString()+"M"; } return str; }