• Android 内部存储(外置SD卡)和扩展存储卡(TF卡)路径的获取
  • 2025-06-03 05:28:53
  • 借用别人的一些话: Android手机上的外置SD卡,起初的时候,即在Android出世的前几年,那时手机的存储是十分有限的,不像现在到处可见16G、32G和64G的存储,因而那时候的手机有的厂商允许插入外置的SD卡,此时这张卡仍处于手机的扩展部分。后来,随着手机的发展以及存储能力的增加,这张外置SD卡,逐渐成为了手机的一部分,不再允许可插拔了,当然现在依然有的手机允许对存储进行拓展,比如三星等。

    那张拓展的存储卡,现在叫做TF卡,且不是所有的手机都支持它,但是有时候有些奇葩需求偏要优先存储在TF卡里面,这叫不得不要求开发人员去检查这张卡是否存在、是否可用。又因为这是手机厂商可拓展、可自定义的部分,所有不同厂商生产的手机,以及同一厂商生产的不同型号的手机,TF卡的位置都相差很大,并没有一个统一的名称或位置。因而这是比较困难的一部分,但是还好Android是开源的,我们可以通过运行时来判断手机是否有TF卡,以及TF卡是否可用。 废话少说,上代码! 1,获取外置SD卡和扩展存储卡TF卡路径的方法类

    /**

    * 获取外置SD卡路径以及TF卡的路径

    *

    * 返回的数据:paths.get(0)肯定是外置SD卡的位置,因为它是primary external storage.

    *

    * @return 所有可用于存储的不同的卡的位置,用一个List来保存

    */

    public static List getExtSDCardPathList() {

    List paths = new ArrayList();

    String extFileStatus = Environment.getExternalStorageState();

    File extFile = Environment.getExternalStorageDirectory();

    //首先判断一下外置SD卡的状态,处于挂载状态才能获取的到

    if (extFileStatus.equals(Environment.MEDIA_MOUNTED)

    && extFile.exists() && extFile.isDirectory()

    && extFile.canWrite()) {

    //外置SD卡的路径

    paths.add(extFile.getAbsolutePath());

    }

    try {

    // obtain executed result of command line code of 'mount', to judge

    // whether tfCard exists by the result

    Runtime runtime = Runtime.getRuntime();

    Process process = runtime.exec("mount");

    InputStream is = process.getInputStream();

    InputStreamReader isr = new InputStreamReader(is);

    BufferedReader br = new BufferedReader(isr);

    String line = null;

    int mountPathIndex = 1;

    while ((line = br.readLine()) != null) {

    // format of sdcard file system: vfat/fuse

    if ((!line.contains("fat") && !line.contains("fuse") && !line

    .contains("storage"))

    || line.contains("secure")

    || line.contains("asec")

    || line.contains("firmware")

    || line.contains("shell")

    || line.contains("obb")

    || line.contains("legacy") || line.contains("data")) {

    continue;

    }

    String[] parts = line.split(" ");

    int length = parts.length;

    if (mountPathIndex >= length) {

    continue;

    }

    String mountPath = parts[mountPathIndex];

    if (!mountPath.contains("/") || mountPath.contains("data")

    || mountPath.contains("Data")) {

    continue;

    }

    File mountRoot = new File(mountPath);

    if (!mountRoot.exists() || !mountRoot.isDirectory()

    || !mountRoot.canWrite()) {

    continue;

    }

    boolean equalsToPrimarySD = mountPath.equals(extFile

    .getAbsolutePath());

    if (equalsToPrimarySD) {

    continue;

    }

    //扩展存储卡即TF卡或者SD卡路径

    paths.add(mountPath);

    }

    } catch (IOException e) {

    e.printStackTrace();

    }

    return paths;

    }

    结果展示:

    有的手机可能有多个TF卡槽,我的手机只有一个, 所以/storage/sdcard1就是我手机扩展TF卡即SD卡的路径 /storage/emulated/0就是手机内的内置存储(即手机内的外置SD卡)路径了!

    2,获取剩余可用容量字节数

    /**

    * 获取指定路径所在空间的剩余可用容量字节数,单位byte

    *

    * @param filePath

    * @return 容量字节 SDCard可用空间,内部存储可用空间

    */

    public static long getFreeBytes(String filePath) {

    // 如果是sd卡的下的路径,则获取sd卡可用容量

    if (filePath.startsWith(getSDCardPath())) {

    filePath = getSDCardPath();

    } else {// 如果是内部存储的路径,则获取内存存储的可用容量

    filePath = Environment.getDataDirectory().getAbsolutePath();

    }

    StatFs stat = new StatFs(filePath);

    long availableBlocks = (long) stat.getAvailableBlocks() - 4;

    return stat.getBlockSize() * availableBlocks;

    // int freeRoot = (int) (SDCardUtils.getFreeBytes(sdpath) / (1024 * 1024)); ---> 获取的是M为单位

    }

    等我有时间,我会整理一下,将项目中用到的东西整理成工具类,或者demo,上传到GitHub上! 请支持我的GITHUB~~谢谢!!!!多点点star吧~~ https://github.com/slyjs