繁体   English   中英

在Android中获取辅助外部存储设备的公共/根目录?

[英]Get the public/root directory of a secondary external storage device in Android?

在 android 中,我可以使用以下方法获取手机的可移动外部存储:

for (File f : context.getExternalFilesDirs("/")) 
    if (Environment.isExternalStorageRemovable(f))
        Log.println(Log.DEBUG, "#", f.getAbsolutePath());

但是,这将返回/storage/8E6A-06FF/Android/data/test.application/files这不是我想要的,因为我只想要可移动的根路径/storage/8E6A-06FF/ 如何获取手机可移动存储的根路径?

你可以试试这个,它对我来说很完美。它在所有操作系统的版本中都很有效。到目前为止,我没有发现这个功能有任何问题。

public static String getSDPath() {
    String filepath = "";
    String[] strPath = {"/storage/sdcard1", "/storage/extsdcard",
            "/storage/sdcard0/external_sdcard", "/mnt/extsdcard",
            "/mnt/sdcard/external_sd", "/mnt/external_sd",
            "/mnt/media_rw/sdcard1", "/removable/microsd", "/mnt/emmc",
            "/storage/external_SD", "/storage/ext_sd",
            "/storage/removable/sdcard1", "/data/sdext", "/data/sdext2",
            "/data/sdext3", "/data/sdext4", "/emmc", "/sdcard/sd",
            "/mnt/sdcard/bpemmctest", "/mnt/sdcard/_ExternalSD",
            "/mnt/sdcard-ext", "/mnt/Removable/MicroSD",
            "/Removable/MicroSD", "/mnt/external1", "/mnt/extSdCard",
            "/mnt/extsd", "/mnt/usb_storage", "/mnt/extSdCard",
            "/mnt/UsbDriveA", "/mnt/UsbDriveB"};

    for (String value : strPath) {
        File f = null;
        f = new File(value);
        if (f.exists() && f.isDirectory()) {
            filepath = value;
            break;
        }
    }
    return filepath;
}

试试这个:

for (File f : context.getExternalFilesDirs("/")) 
    if (Environment.isExternalStorageRemovable(f))
        Log.println(Log.DEBUG, "#", f.getParentFile().getParentFile().getParentFile().getParent());

context.getExternalFilesDirs() 将始终返回特定于应用程序的目录。 但好消息是,特定于应用程序的目录始终距存储设备的根文件夹深 4 级。 因此,在 File f 上调用 getParentFile() 四次而不是 f.getAbsolutePath() 将使您获得手机可移动存储的根路径。

也许只是在Android拆分它?

我测试了它,它在我 请求许可后工作- WRITE_EXTERNAL_STORAGE

fun getBaseDir(dir: File): String {
    val absPath = dir.absolutePath
    return if (absPath.contains("/Android")) {
        absPath.split("/Android")[0]
    } else {
        absPath
    }
}

这将遍历 sdcard 根目录中的文件。 如果您想要主存储,只需将 [1] 更改为 [0]。 getExternalFilesDirs返回主存储和辅助存储上应用程序目录的路径。 通过“Android”拆分第二个路径后,第一个字符串将包含您的辅助存储根目录的路径。 例如,在我的情况下,它是“/storage/B242-37B2/”。 使用minSdkVersion 19+

            String sdCardRoot = ContextCompat.getExternalFilesDirs(getApplicationContext(), null)[1].getAbsolutePath().split("Android")[0];

            File f = new File(sdCardRoot);
            File[] files = f.listFiles();

            for (File inFile : files){
                Log.d("Files", inFile.getName());
            }

如果它对你有帮助,试试这个。 有关更多信息,请参阅此链接

public static HashSet<String> getExternalMounts() {
    final HashSet<String> out = new HashSet<String>();
    String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
    String s = "";
    try {
        final Process process = new ProcessBuilder().command("mount")
                .redirectErrorStream(true).start();
        process.waitFor();
        final InputStream is = process.getInputStream();
        final byte[] buffer = new byte[1024];
        while (is.read(buffer) != -1) {
            s = s + new String(buffer);
        }
        is.close();
    } catch (final Exception e) {
        e.printStackTrace();
    }

    // parse output
    final String[] lines = s.split("\n");
    for (String line : lines) {
        if (!line.toLowerCase(Locale.US).contains("asec")) {
            if (line.matches(reg)) {
                String[] parts = line.split(" ");
                for (String part : parts) {
                    if (part.startsWith("/"))
                        if (!part.toLowerCase(Locale.US).contains("vold"))
                            out.add(part);
                }
            }
        }
    }
    return out;
}

这是另一种方法。 来源从这里

Environment.getExternalStorageState() 返回内部 SD 挂载点的路径,如“/mnt/sdcard”

但问题是关于外部 SD。 如何获得像“/mnt/sdcard/external_sd”这样的路径(它可能因设备而异)?

如上所述,除了外部存储之外,Android 没有“外部 SD”的概念。

如果设备制造商选择将外部存储作为板载闪存并且也有 SD 卡,您将需要联系该制造商以确定您是否可以使用 SD 卡(不保证)以及规则是什么使用它,例如使用什么路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM