Android学习之获取外置SD卡读写路径方法详解
来源:互联网 发布:mac好玩的免费游戏 编辑:程序博客网 时间:2024/05/24 05:15
本文和大家分享的主要是android系统获取外置SD卡读写路径相关内容,一起来看看吧,希望对大家学习android有所帮助。
1. 外置SD卡的一些问题
1.1 关于外置SD卡上的读写路径
Android 4.4 及以上版本,应用的外置SD卡读写路径被限定在固定路径上( 外置SD卡根路径/Android/data/包名/files )。
Android4.4 以下版本,申请了外置SD卡读写权限的应用在整个外置SD卡上都有读写权限。
1.2 关于外置SD卡路径
另外 Android 没有提供获取外置SD卡路径的API( getExternalStorageDirectory() 获取的实际是内置SD卡路径)。
2. 获取应用在外置SD卡读写根路径
在 Android 4.4 以下版本,获取的应该是外置SD卡的根目录(类似 /storage/sdcard1 )。在 Android 4.4 及以上版本,获取的是应用在SD卡上的限定目录( 外置SD卡根路径/Android/data/包名/files/file )
代码如下:
public static String getExternalSDPath(Context aContext) {
String root = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
root = getExternalSDPathKITKAT(aContext);
File f = new File(root);
if (!f.exists()) {
try {
f.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
if (!f.exists()) {
root = null;
}
}
} else {
root = getExternalSDCardPath(aContext);
}
return root;
}
// Android 4.4及以上版本,获取软件在外置SD卡上的保存路径
public static String getExternalSDPathKITKAT(Context aContext) {
String rootPath = getStoragePath(aContext, true);
if (TextUtils.isEmpty(rootPath)) {
return null;
}
File f = new File(rootPath, "Android/data/" + aContext.getPackageName() + "/files/file");
String fpath = f.getAbsolutePath();
return fpath;
}
// Android 4.4 以下版本获取外置SD卡根目录
public static String getExternalSDCardPath(Context aContext) {
HashSetpaths = getExternalMounts();
File defaultPathFile = aContext.getExternalFilesDir(null);
String defaultPath;
if (defaultPathFile == null) {
return null;
} else {
defaultPath = defaultPathFile.getAbsolutePath();
}
String prefered = null;
for (Iterator it = paths.iterator(); it.hasNext();) {
String path = (String) (it.next());
if (prefered == null && !defaultPath.startsWith(path)) {
prefered = path;
}
}
return prefered;
}
来源:极客头条
1. 外置SD卡的一些问题
1.1 关于外置SD卡上的读写路径
Android 4.4 及以上版本,应用的外置SD卡读写路径被限定在固定路径上( 外置SD卡根路径/Android/data/包名/files )。
Android4.4 以下版本,申请了外置SD卡读写权限的应用在整个外置SD卡上都有读写权限。
1.2 关于外置SD卡路径
另外 Android 没有提供获取外置SD卡路径的API( getExternalStorageDirectory() 获取的实际是内置SD卡路径)。
2. 获取应用在外置SD卡读写根路径
在 Android 4.4 以下版本,获取的应该是外置SD卡的根目录(类似 /storage/sdcard1 )。在 Android 4.4 及以上版本,获取的是应用在SD卡上的限定目录( 外置SD卡根路径/Android/data/包名/files/file )
代码如下:
public static String getExternalSDPath(Context aContext) {
String root = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
root = getExternalSDPathKITKAT(aContext);
File f = new File(root);
if (!f.exists()) {
try {
f.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
if (!f.exists()) {
root = null;
}
}
} else {
root = getExternalSDCardPath(aContext);
}
return root;
}
// Android 4.4及以上版本,获取软件在外置SD卡上的保存路径
public static String getExternalSDPathKITKAT(Context aContext) {
String rootPath = getStoragePath(aContext, true);
if (TextUtils.isEmpty(rootPath)) {
return null;
}
File f = new File(rootPath, "Android/data/" + aContext.getPackageName() + "/files/file");
String fpath = f.getAbsolutePath();
return fpath;
}
// Android 4.4 以下版本获取外置SD卡根目录
public static String getExternalSDCardPath(Context aContext) {
HashSetpaths = getExternalMounts();
File defaultPathFile = aContext.getExternalFilesDir(null);
String defaultPath;
if (defaultPathFile == null) {
return null;
} else {
defaultPath = defaultPathFile.getAbsolutePath();
}
String prefered = null;
for (Iterator it = paths.iterator(); it.hasNext();) {
String path = (String) (it.next());
if (prefered == null && !defaultPath.startsWith(path)) {
prefered = path;
}
}
return prefered;
}
来源:极客头条
0 0
- Android学习之获取外置SD卡读写路径方法详解
- Android获取外置SD卡读写路径
- Android获取内置和外置sd卡路径的方法
- Android 6.0获取外置SD卡路径方法
- Android 4.0获取外置SD卡 路径
- android 获取外置SD卡的路径
- android 手机获取外置SD卡路径
- android获取外置SD卡的路径
- Android 获取外置SD卡路径
- android获取外置SD卡路径
- android获取外置sd卡路径
- Android获取外置SD卡路径
- Anroid获取内置外置SD卡路径以及读写测试
- Android中获取内置SD卡和外置SD卡路径的方法
- android 获取外置SD卡的路径(非挫版)
- android 获取外置SD卡的路径(非挫版)
- android 获取外置SD卡的路径(非挫版)
- android获取内置和外置SD卡路径
- CentOS 图形界面的关闭与开启
- FreeMarker语法知识
- hibernate主配置文件
- ViewPager无限自动轮播小圆点+网络加载图片+活动倒计时
- [bzoj1500] 维修数列
- Android学习之获取外置SD卡读写路径方法详解
- Windows编程_Lesson004_Windows消息机制
- 21 idea_maven 下载不了代码
- sql-server 创建
- C语言指针
- 经典sql面试题
- 【java web】实现判断是由手机访问还是由电脑访问
- 红外图像非均匀矫正——一点矫正
- 开发windows服务