【问题标题】:Environment.getExternalStorageDirectory().getAbsolutePath() return sdcard0 but I have my SD in sdcard1Environment.getExternalStorageDirectory().getAbsolutePath() 返回 sdcard0 但我的 SD 在 sdcard1
【发布时间】:2016-04-18 12:12:22
【问题描述】:

我尝试使用 Environment.getExternalStorageDirectory().getAbsolutePath() 在 SD 卡手机内存中写入一些内容以获取 SD 路径,结果是 /storage/sdcard0。但我的 SD 卡是 /storage/sdcard1。如何获取真正的 SD 卡路径?

我可以在“/”中看到一个名为 /ext_sd 的符号链接链接到 /storage/sdcard1。这个符号链接在所有设备中?

【问题讨论】:

  • 请参阅Android: Finding the SD Card Path 获取三星设备上的外部 sdcard 路径
  • 您面临的基本问题是额外外部存储卷的处理是设备/供应商唯一的。

标签: android android-sdcard


【解决方案1】:

我在这篇文章中找到了解决方案:How can I get external SD card path for Android 4.0+?

/**
 * Returns the list of external devices mounted.
 * 
 * @return HashSet<String>.
 */
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;
}

【讨论】:

  • 这很愚蠢 - 不要将 mount 命令作为进程启动,将 /proc/mounts 读取为文本文件。
【解决方案2】:

试试这个,

  File filepath = Environment.getExternalStorageDirectory();

                File dir = new File(filepath.getAbsolutePath()
                        + "/Save PIP/");
                dir.mkdirs();


                File file = new File(dir, System.currentTimeMillis() + ".jpg");


               // Utils.showAlert("Image Saved to SD Card", "PIP Effect", "Ok", FrameActivity.this);

                if (file.exists()) file.delete();
                try {

                    output = new FileOutputStream(file);
                    bitmap_final.compress(Bitmap.CompressFormat.PNG, 100, output);
                    output.flush();
                    output.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

在这里,你可以得到你的 sd 卡路径。我希望它能帮助你和你尽快解决你的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多