Android获取外置SD卡读写路径
2017-04-21 14:38
375 查看
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) { HashSet<String> paths = 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; }
相关文章推荐
- Android获取拓展外置SD卡(可插拔)路径及读写外置SD卡的方法
- Android学习之获取外置SD卡读写路径方法详解
- android 手机获取外置SD卡路径
- android获取内置和外置SD卡路径
- Android 内部存储(外置SD卡)和扩展存储卡(TF卡)路径的获取
- android 获取文件路径(内置sd卡和外置sd卡)。
- Android获取机身存储、内置SD卡与外置TF卡路径
- Android获取不同手机 自身存储和外置SD卡存储路径的解决方案
- Android 6.0获取外置SD卡路径方法
- android 获取外置SD卡的路径(非挫版)
- android获取内置和外置SD卡路径 - z
- android获取内置和外置SD卡路径
- Android4.4+ 外置SD卡不能写入 获取外置SD卡路径解决方案
- Android判断是否存在外置SD卡(获取手机所有存储设备的路径)
- android两行代码获取内外置sd卡路径
- Android 获取外置SD卡路径
- android获取内置和外置SD卡路径
- android4.x获取(也可监测)外置sd路径和读写
- Android开发获取多个存储空间的路径(内置SD卡以及外置SD卡)
- android获取内置和外置SD卡路径