因为SD卡操作相关功能最近用的比较多,所以集合了一个SD卡相关操作的工具类;
记得加上SD卡权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
已下为正文:
public class SDToolBiz{ //创建文件夹 public static void createPath(String strPath) { File file = new File(strPath); if (!file.exists()) { file.mkdir(); } } /* 获取当前文件夹下的所有图片 其他文件同理 删除文件时将list.add(fi.getPath())替换为fi.delete(); */ public static List<String> getFileAllPictures(String strPath) { List<String> list = new ArrayList<String>(); File file = new File(strPath); File[] allfiles = file.listFiles(); if (allfiles == null) { return null; } for (int k = 0; k < allfiles.length; k++) { final File fi = allfiles[k]; if (fi.isFile()) { int idx = fi.getPath().lastIndexOf("."); if (idx <= 0) { continue; } String strP = fi.getPath().substring(idx); if (strP.toLowerCase().equals(".jpg") || strP.toLowerCase().equals(".jpeg") || strP.toLowerCase().equals(".bmp") || strP.toLowerCase().equals(".png") || strP.toLowerCase().equals(".gif")) { list.add(fi.getPath()); } } } return list; } //获取当前文件夹下的所有文件 public static void getAllFiles(File file){ File files[] = file.listFiles(); if(files != null){ for (File f : files){ if(f.isDirectory()){ getAllFiles(f); }else{ System.out.println(f); } } } } //删除废数据(递归) public static void deleteFile(File file) { if (file.exists() == false) { return; } else { if (file.isFile()) { file.delete(); return; } if (file.isDirectory()) { File[] childFile = file.listFiles(); if (childFile == null || childFile.length == 0) { file.delete(); return; } for (File f : childFile) { deleteFilePicture(f); } file.delete(); } } } File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/" + olds + ".png"); File newfile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/" + news + ".png"); //删除文件 文件夹通用 if(file.exists()){ file.delete(); } //重命名文件 if (file.exists()) { file.renameTo(newFile); } }