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方式),否则创建目录不成功;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值