// 判断sd卡是否存在 boolean sdCardExist = Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); // 获得sd卡根目录 if (sdCardExist) { File dir = Environment.getExternalStorageDirectory(); String path = dir.getAbsolutePath() + "/麻城市宅基地资料/"; ArrayList<String> folder = getFolder(path); listParents = listParent(folder); }
------------------------
// List转换为String[] public String[] listParent(ArrayList<String> list) { int size = list.size(); String[] array = new String[size]; for (int i = 0; i < list.size(); i++) { array[i] = (String) list.get(i); } return array; } // 检查文件夹是否存在 boolean isFolderExists(String strFolder) { File file = new File(strFolder); return file.exists(); } // 扫描文件夹目录下的文件夹名字 public ArrayList<String> getFolder(String path) { ArrayList<String> names = new ArrayList<String>(); File file = new File(path); if (file.isDirectory()) { File[] array = file.listFiles(); for (int i = 0; i < array.length; i++) { File f = array[i]; names.add(f.getName()); System.out.println("文件夹名称" + f.getName()); } } return names; } // 扫描文件夹里面的数据 public ArrayList<String> folderScan(String path) { File file = new File(path); ArrayList<String> names = new ArrayList<String>(); if (file.isDirectory()) { File[] array = file.listFiles(); for (int i = 0; i < array.length; i++) { File f = array[i]; if (f.isFile()) {// FILE TYPE String name = f.getName(); if (name.contains(".jpg")) { names.add(name); fileScan(f.getAbsolutePath()); } } else {// FOLDER TYPE folderScan(f.getAbsolutePath()); } } } return names; } // 扫描 public void fileScan(String file) { Uri data = Uri.parse("file://" + file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); }
// 重命名文件 public void renameFloder(String floerName, String reName) { File file = new File(floerName); System.out.println("-->" + file.toString()); System.out.println("-->" + rename); file.renameTo(new File(reName)); }