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 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;    }

更多相关文章

  1. android 兼容各个版本 读取通讯录
  2. android通过反射获取内置外置存储器列表
  3. Android(1.1-4.2) platform 开发包【全版本】
  4. Android studio 修改SDK路径
  5. 火爆新东西,仿QQ版本的ResideMenuItem框架(最新QQ版本的)
  6. 版本更新
  7. Ubuntu 12.04 Desktop 版本编译 Android 4.0.4 出错解决
  8. Android通过Uri获取文件的路径的方法
  9. 【Android】获取apk的版本及包名等信息

随机推荐

  1. android蓝牙开发入门到精通3---服务端客
  2. android中的Sqlite数据库操作
  3. Android(安卓)修改默认按键不震动
  4. android spinner下拉选择框
  5. android 存储一些app配置信息
  6. android 使用md5加密
  7. Android 发布项目到私服Nexus仓库
  8. Android用surface直接显示yuv数据
  9. android 8.1 蓝牙打不开之CLOCK_BOOTTIME
  10. Android中对Group的各种操作示例代码