获取的Android二次外部存储设备的公共/根目录?

问题描述 投票:1回答:4

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/什么。我如何能得到我的手机的移动存储设备的根路径?

android android-external-storage
4个回答
1
投票

你可以试试这个它是完全适用于me.It就像一个魅力与所有操作系统的version.I没有发现任何问题,到目前为止,使用此功能。

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

1
投票

尝试这个:

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

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


0
投票

也许只是在Android拆呢?

我测试了它,和它的作品我request for permission后 - WRITE_EXTERNAL_STORAGE

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

-1
投票

试试这一个,如果它可以帮助你。欲了解更多信息,请参阅this link

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

下面是相同的另一种方法。资料来源here

Environment.getExternalStorageState()返回路径内部SD安装像“的/ mnt / SD卡”点

但问题是关于外部SD。如何获得像“到/ mnt / SD卡/ external_sd”的路径(它可能会有所不同从设备到设备)?

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

如果设备制造商已选择具有外部存储是板上闪存,也有一个SD卡,您将需要联系制造商以确定您是否能使用SD卡(不保证)和规则是什么的使用它,比如什么路径来使用它。

© www.soinside.com 2019 - 2024. All rights reserved.