所有设备的android sd卡路径

时间:2015-02-08 14:48:13

标签: android

我尝试使用此代码获取sd卡路径来存储图像:
 

String filepath = Environment.getExternalStorageDirectory().getAbsolutePath(); 

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

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

我使用此代码成功完成了它:

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