【问题标题】:android sd card path for all devices所有设备的android sd卡路径
【发布时间】:2015-03-08 23:06:59
【问题描述】:

我尝试使用以下代码获取存储图像的 sd 卡路径:

字符串文件路径 = Environment.getExternalStorageDirectory().getAbsolutePath(); 

但它不适用于所有设备和所有版本的 android。

【问题讨论】:

标签: android


【解决方案1】:

试试这个:

String sdcardPath = Environment.getExternalStorageDirectory().getPath();

【讨论】:

  • 部分银河手机出现同样的问题
【解决方案2】:

我使用这段代码成功地做到了:

public File getSDPath() {
    String filepath = Environment.getExternalStorageDirectory()
            .getAbsolutePath();

    if (android.os.Build.DEVICE.contains("samsung")
            || android.os.Build.MANUFACTURER.contains("samsung")) {
        File f = new File(Environment.getExternalStorageDirectory()
                .getParent() + "/extSdCard");
        if (f.exists() && f.isDirectory()) {
            try {
                File file = new File(f, "test");
                FileOutputStream fos = new FileOutputStream(file);
                filepath = Environment.getExternalStorageDirectory()
                        .getParent() + "/extSdCard";
            } catch (FileNotFoundException e) {
            }

        } else {
            f = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/external_sd");
            if (f.exists() && f.isDirectory()) {
                try {
                    File file = new File(f, "test");
                    FileOutputStream fos = new FileOutputStream(file);
                    filepath = Environment.getExternalStorageDirectory()
                            .getAbsolutePath() + "/external_sd";
                } catch (FileNotFoundException e) {
                }

            }
        }
    }

    return new File(filepath);
}

【讨论】:

  • 这不适用于所有设备,例如在我的中国手机上,可移动 SD 卡位于 /mnt/extsd/
  • 没错,但我没有更好的解决方案。如果有人可以帮助我们,我将不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多