Android获取SDcard目录及创建文件夹的方法
作者:wolf犭良 时间:2022-03-03 04:16:31
获取sdcard目录
public static String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);// 判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
}
return sdDir.toString();
}
创建目录,不限目录层级
public static String mkdirs(String path) {
String sdcard = getSDPath();
if (path.indexOf(getSDPath()) == -1) {
path = sdcard + (path.indexOf("/") == 0 ? "" : "/") + path;
}
File destDir = new File(path);
if (!destDir.exists()) {
path = makedir(path);
if (path == null) {
return null;
}
}
return path;
}
private static String makedir(String path) {
String sdPath = getSDPath();
String[] dirs = path.replace(sdPath, "").split("/");
StringBuffer filePath = new StringBuffer(sdPath);
for (String dir : dirs) {
if (!"".equals(dir) && !dir.equals(sdPath)) {
filePath.append("/").append(dir);
File destDir = new File(filePath.toString());
if (!destDir.exists()) {
boolean b = destDir.mkdirs();
if (!b) {
return null;
}
}
}
}
return filePath.toString();
}
所需权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 在sdcard中创建/删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
显示本地图片
ImageView view5 = findView(R.id.imageview2);
view5.setImageBitmap(ImageUtils.getLoacalBitmap("/storage/sdcard1/myimage/20160807.jpg"));
public static Bitmap getLoacalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
//这里应显示默认图片,如图片无法显示等;从应用资源图片中选取
return null;
}
}
来源:https://blog.csdn.net/ldz_wolf/article/details/52139889
标签:Android,SDcard,文件夹
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现TopK问题的方法
2023-11-10 20:32:14
android 引导界面的实现方法
2023-08-07 04:58:11
java自定义切面增强方式(关于自定义注解aop)
2022-05-30 15:16:58
通过面试题解析 Java 类加载机制
2022-08-13 12:49:16
intellij idea如何将web项目打成war包的实现
2023-05-27 23:12:27
![](https://img.aspxhome.com/file/2023/0/75520_0s.png)
Java下SpringBoot创建定时任务详解
2023-10-03 01:25:23
![](https://img.aspxhome.com/file/2023/7/82307_0s.png)
Spring Boot学习入门之AOP处理请求详解
2023-11-27 10:55:17
WinForm中的几个实用技巧汇总
2022-08-12 13:17:02
SpringBoot结合Redis配置工具类实现动态切换库
2022-04-15 14:14:13
详解在Spring中如何使用AspectJ来实现AOP
2021-10-23 02:06:06
关于synchronized有趣的同步问题
2021-11-03 05:46:55
java中javamail收发邮件实现方法
2022-12-15 02:03:03
![](https://img.aspxhome.com/file/2023/4/68714_0s.png)
android采用FFmpeg实现音频混合与拼接剪切
2023-10-04 06:51:24
![](https://img.aspxhome.com/file/2023/4/87274_0s.jpg)
Java正则验证IP的方法实例分析【测试可用】
2023-05-25 03:28:57
![](https://img.aspxhome.com/file/2023/9/72619_0s.jpg)
详解java nio中的select和channel
2021-08-09 19:17:39
创建动态代理对象bean,并动态注入到spring容器中的操作
2021-09-04 01:02:43
![](https://img.aspxhome.com/file/2023/9/71589_0s.jpg)
Mybatis 如何开启控制台打印sql语句
2023-08-02 02:01:53
![](https://img.aspxhome.com/file/2023/5/99865_0s.jpg)
mybatisplus之Wrappers.ne踩坑记录解决
2021-08-28 10:13:13
Springboot+AOP实现返回数据提示语国际化的示例代码
2021-08-18 19:49:12
![](https://img.aspxhome.com/file/2023/5/73125_0s.png)
java生成XML的方法
2023-08-12 11:45:46