Android创建文件夹和文件

1 // 电子证据存放基本路径
2 private static String basePath;
3 // 电子证据存放根路径
4 private static String evidencePath;
5 // 照片基本存放路径
6 private static String imageBasePath;
 1 /**
2 * 1、判断SD卡是否存在
3 */
4 public static boolean hasSdcard() {
5 String status = Environment.getExternalStorageState();
6 if (status.equals(Environment.MEDIA_MOUNTED)) {
7 return true;
8 } else {
9 return false;
10 }
11 }
 1 /**
2 * 2、获取电子证据存放根路径
3 */
4 public static String getBasePath() {
5 if (StringUtils.isNullOrEmpty(basePath)) {
6 if (MultimediaHelper.hasSdcard()) {
7 basePath = EnvironmentShare.getSdCardAbsolutePath();
8 } else {
9 basePath = "";
10 }
11 FileUtils.createPath(basePath);
12 }
13 return basePath;
14 }
 1 /**
2 * 3、获取(创建)电子证据存放基本路径
3 */
4 public static String getEvidencePath() {
5 if (StringUtils.isNullOrEmpty(evidencePath)) {
6 evidencePath = getBasePath() + "/evidence/";
7 FileUtils.createPath(evidencePath);
8 }
9 return evidencePath;
10 }
 1 /**
2 * 4、获取(创建)照片基本存放路径
3 */
4 public static String getImageBasePath() {
5 if (StringUtils.isNullOrEmpty(imageBasePath)) {
6 imageBasePath = getEvidencePath() + "images/";
7 FileUtils.createPath(imageBasePath);
8 }
9 return imageBasePath;
10 }
1 /**
2 * 5、创建目录
3 */
4 public static void createPath(String path) {
5 File file = new File(path);
6 if (!file.exists()) {
7 file.mkdir();
8 }
9 }
6、添加权限
<!--往sdcard中写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!--在sdcard中创建/删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

【注意】

1、每次只能创建一级目录,不能连续创建多级目录;

2、文件夹路径分隔符必须是"/"(Linux方式),不能是"\"(Windows方式),否则创建目录不成功;







原文地址:https://www.cnblogs.com/springworks/p/2212517.html