【问题标题】:How to access removable storage on Android devices?如何访问 Android 设备上的可移动存储?
【发布时间】:2012-04-23 14:24:15
【问题描述】:

我正在寻找一种方法来检测和访问各种 Android 设备(三星、摩托罗拉、LG、索尼、HTC)上的可移动 sd 卡。

我还需要与 2.2 兼容,因此我无法使用 Environment.isExternalStorageRemovable()

摩托罗拉有自己的库,对于三星我可以检测到/external_sd/的存在

我对其他人一无所知。例如,我在一些 LG 上看到了 /_ExternalSD/,但即使删除了 SD,该目录仍然存在。

一个额外的问题:ACTION_MEDIA_MOUNTED 意图是否会被广播给他们中的任何一个

对此的任何提示都会非常有帮助。

【问题讨论】:

  • Environment 上的方法之外的任何东西都超出了 SDK 的范围并且将是不可靠的。
  • 但是鉴于没有更可靠的方法可以通过 API 访问可移动存储,如果需要访问 SD 卡,那么任何最有效的方法总比没有好。

标签: android android-sdcard


【解决方案1】:

这是我用来查找设备上所有 sdcard 的类;内置和可拆卸。我一直在冰淇淋三明治上使用它,但它应该可以在 2 倍的水平上工作。

public class GetRemovableDevice {

private final static String TAG = "GetRemoveableDevice";

public GetRemovableDevice() {
}

public static String[] getDirectories() {
    MyLog.d(TAG, "getStorageDirectories");
    File tempFile;
    String[] directories = null;
    String[] splits;
    ArrayList<String> arrayList = new ArrayList<String>();
    BufferedReader bufferedReader = null;
    String lineRead;

    try {
        arrayList.clear(); // redundant, but what the hey
        bufferedReader = new BufferedReader(new FileReader("/proc/mounts"));

        while ((lineRead = bufferedReader.readLine()) != null) {
            MyLog.d(TAG, "lineRead: " + lineRead);
            splits = lineRead.split(" ");

            // System external storage
            if (splits[1].equals(Environment.getExternalStorageDirectory()
                    .getPath())) {
                arrayList.add(splits[1]);
                MyLog.d(TAG, "gesd split 1: " + splits[1]);
                continue;
            }

            // skip if not external storage device
            if (!splits[0].contains("/dev/block/")) {
                continue;
            }

            // skip if mtdblock device

            if (splits[0].contains("/dev/block/mtdblock")) {
                continue;
            }

            // skip if not in /mnt node

            if (!splits[1].contains("/mnt")) {
                continue;
            }

            // skip these names

            if (splits[1].contains("/secure")) {
                continue;
            }

            if (splits[1].contains("/mnt/asec")) {
                continue;
            }

            // Eliminate if not a directory or fully accessible
            tempFile = new File(splits[1]);
            if (!tempFile.exists()) {
                continue;
            }
            if (!tempFile.isDirectory()) {
                continue;
            }
            if (!tempFile.canRead()) {
                continue;
            }
            if (!tempFile.canWrite()) {
                continue;
            }

            // Met all the criteria, assume sdcard
            arrayList.add(splits[1]);
        }

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
            }
        }
    }

    // Send list back to caller

    if (arrayList.size() == 0) {
        arrayList.add("sdcard not found");
    }
    directories = new String[arrayList.size()];
    for (int i = 0; i < arrayList.size(); i++) {
        directories[i] = arrayList.get(i);
    }
    return directories;
}

}

MyLog.d 是一个扩展 Log.d 的跟踪类 - 它可以被删除。

该类读取 /proc/mounts/ 并且:

  1. 检查路径名是否为内部sdcard目录
  2. 检查它是否是块设备
  3. 跳过 mtdblock 设备
  4. 跳过任何未安装的内容
  5. 跳过安全和 asec 目录
  6. 确保它存在,是一个目录,并且可以读/写访问

如果所有这些都匹配,它假定您有一个 sdcard 并将路径添加到数组列表中。它返回一个路径名的字符串数组。

要调用 getDirectories 函数,代码类似于:

String[] sdcardDirectories = GetRemoveableDevice.getDirectories();

返回的路径可用于创建用户选择列表、扫描文件等。

最后,这是来自模拟器测试的两行 MyLog.d(第二行是模拟器 sdcard):

09-19 15:57:12.511: D/GetRemoveableDevice(651): lineRead: /dev/block/mtdblock2 /cache yaffs2 rw,nosuid,nodev 0 0

09-19 15:57:12.511: D/GetRemoveableDevice(651): lineRead: /dev/block/vold/179:0 /mnt/sdcard vfat rw,dirsync,nosuid,nodev,noexec,uid=1000, gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0

【讨论】:

  • 哎呀。我忘了提到检查读/写状态会消除在 /mnt/ 目录中仍有条目的未挂载 sdcard。
  • 我知道这个线程很旧,但我正在将一些存储检测代码转换为通用库,并希望区分永久的外部存储和实际可移动的外部存储。不知道你的解决方案是否满足这个要求?似乎循环中的第一个“if”语句将存储“getExternalStorageDirectory()”返回的路径的任何出现,这通常不是物理可移动的,对吧?
  • 抱歉拖了这么久。低头在键盘上处理项目。要回答,是的,getExtenalStorageDirectory 的路径通常是不可安装的 sdcard。返回的列表中的第一个条目是 getExternal sdcard。如果列表有多个条目,请使用第二个或后面的条目。您还可以检查它是否可以卸载。此外,您可以创建一个简单的项目来显示列表(以及您想要的任何其他内容),然后检查当您移除和替换可移除的 sdcard 时会发生什么。
  • 不能在 Galaxy S3 上开箱即用。我做了一些改变。请参阅下面的答案。
【解决方案2】:

基于 Howards 课程,我做了一些修改以使其在 Galaxy S3 上运行。

  1. Environment.getExternalStorageDirectory() 返回 S3 上的内部存储。
  2. 可移动存储不一定安装在 /mnt 下
  3. 可移动媒体必须具有 vfat 文件系统

_

public static String getDirectory() {
        Log.d(TAG, "getStorageDirectories");
        File tempFile;
        String[] splits;
        ArrayList<String> arrayList = new ArrayList<String>();
        BufferedReader bufferedReader = null;
        String lineRead;

        try {
            arrayList.clear(); // redundant, but what the hey
            bufferedReader = new BufferedReader(new FileReader("/proc/mounts"));

            while ((lineRead = bufferedReader.readLine()) != null) {
                Log.d(TAG, "lineRead: " + lineRead);
                splits = lineRead.split(" ");

                // skip if not external storage device
                if (!splits[0].contains("/dev/block/")) {
                    continue;
                }

                // skip if mtdblock device
                if (splits[0].contains("/dev/block/mtdblock")) {
                    continue;
                }

                // skip if not in vfat node
                if (!splits[2].contains("vfat")) {
                    continue;
                }

                // skip these names
                if (splits[1].contains("/secure")) {
                    continue;
                }

                if (splits[1].contains("/mnt/asec")) {
                    continue;
                }

                // Eliminate if not a directory or fully accessible
                tempFile = new File(splits[1]);
                if (!tempFile.exists()) {
                    continue;
                }
                if (!tempFile.isDirectory()) {
                    continue;
                }
                if (!tempFile.canRead()) {
                    continue;
                }
                if (!tempFile.canWrite()) {
                    continue;
                }

                // Met all the criteria, assume sdcard
                return splits[1];
            }

        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                }
            }
        }

        return null;
    }

【讨论】:

  • 我同意这是一个 hack。但这是我见过的最干净的 hack。如果您找到合适的方法,请告诉我们。
  • 和我发的有什么不同
  • 它是从虚拟文件中读取而不是生成进程。
【解决方案3】:

基于 Howards 课程,我进一步修改了该课程,因为我注意到我能找到的几部手机和平板电脑上的所有外部可移动存储都是使用卷安装守护程序 vold 安装的。

            // skip if not external storage device
            if (!splits[0].contains("vold")) {
                continue;
            }

            if (splits[1].contains("/mnt/asec")) {
                continue;
            }

            // Eliminate if not a directory or fully accessible
            tempFile = new File(splits[1]);
            if (!tempFile.exists()) {
                continue;
            }
            if (!tempFile.isDirectory()) {
                continue;
            }
            if (!tempFile.canRead()) {
                continue;
            }
            if (!tempFile.canWrite()) {
                continue;
            }

            // Met all the criteria, assume sdcard
            arrayList.add(splits[1]);

【讨论】:

  • 感谢您分享所学知识。 +1。请记住,“以上”不是答案的稳定属性...目前此页面上还有 3 个其他类,当有人查看此页面时,您所指的 2 个类可能会出现在您的下方。
【解决方案4】:

这些功能适用于所有 Android 版本:

  • 要获取应用程序在外部存储上的文件夹,请调用Context.getExternalFilesDir

  • 请记住,您的应用需要明确的权限才能访问外部存储,并且您应该通过Environment.getExternalStorageState检查它是否可用

  • 是的,只要可以访问可移动媒体,就会广播ACTION_MEDIA_MOUNTED(您还应该收听ACTION_MEDIA_EJECTACTION_MEDIA_REMOVED

【讨论】:

  • 感谢 Tony,但这只会返回 /mnt/sdcard,它不是可移动存储设备。我已经编辑了我的问题,所以它是准确的。
  • 是的,外部存储通常是可移动的,但不是总是可移动的。您的具体用例是什么?
  • 应用程序必须提供可供下载的存储设备选择。通常是内部与外部可移动存储设备。从我看到的分离不是那么清楚。例如。三星将可移动 sd 安装在 /mnt/sdcard/external_sd 上。 LG 在 /mnt/sdcard/_ExternalSD/ 上,但即使拔下可移动设备,目录和一些文件仍然存在。这表明没有逻辑分离
  • 你是对的,2.3之前没有可移动/不可移动的区别。是否有任何实际设备具有两种类型的存储(并且未运行 2.3 或更高版本)?
  • 其内置外置存储的挂载路径是什么?具体来说,Context.getExternalFilesDir 返回哪个路径?
【解决方案5】:

这是我创建并正在使用的方法。这已在三星 Galaxy S4、三星 Galaxy Note 3 和索尼 Xperia Z2 上运行。

private static String[] getRemovableStoragePaths() {
    String[] directories;
    String[] splits;
    ArrayList<String> pathList = new ArrayList<String>();
    BufferedReader bufferedReader = null;
    String lineRead;

    try {
        bufferedReader = new BufferedReader(new FileReader("/proc/mounts"));

        while ((lineRead = bufferedReader.readLine()) != null) {
            Log.d(TAG, "lineRead: " + lineRead);
            splits = lineRead.split(" ");
            Log.d(TAG, "Testing path: " + splits[1]);

            if (!splits[1].contains("/storage")) {
                continue;
            }

            if (splits[1].contains("/emulated")) {
                // emulated indicates an internal storage location, so skip it.
                continue;
            }

            // Eliminate if not a directory or fully accessible
            Log.d(TAG, "Path found: " + splits[1]);

            // Met all the criteria, assume sdcard
            pathList.add(splits[1]);
        }

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
            }
        }
    }

    // Send list back to caller

    if (pathList.size() == 0) {
        pathList.add("sdcard not found");
    } else {
        Log.d(TAG, "Found potential removable storage locations: " + pathList);
    }
    directories = new String[pathList.size()];
    for (int i = 0; i < pathList.size(); i++) {
        directories[i] = pathList.get(i);
    }
    return directories;
}

【讨论】:

    猜你喜欢
    • 2020-07-05
    • 2010-10-07
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2017-03-20
    相关资源
    最近更新 更多