Environment.getExternalStorageDirectory不返回的路径移动存储移动存储、路径、Environment、getExternalStorageDirectory

2023-09-12 07:04:04 作者:意中人是位盖世垃圾

由于API 8级,看来Android已经重新定义了外部存储器是什么。通过阅读http://developer.android.com/reference/android/os/Environment.html,连接到文档 getExternalStorageDirectory 我看到的评论:不要被这个词外部这里这个目录可以更好地被认为是媒体/共享存储混淆。 ..在有多个外部存储目录...设备,这个目录重新presents的'主'外部存储,用户将互动。

As of API level 8, it seems Android has redefined what "external" storage is. Reading through http://developer.android.com/reference/android/os/Environment.html, attached to the documentation for getExternalStorageDirectory I see the comment: "don't be confused by the word 'external' here. This directory can better be thought as media/shared storage... In devices with multiple 'external' storage directories... , this directory represents the 'primary' external storage that the user will interact with."

我的应用程序写入的文件由 getExternalStorageDirectory 获得的路径,我有用户问一个选项来写自己的可移动SD卡代替。我一直认为 getExternalStorageDirectory 返回的路径可移动SD卡,但是这一点已不再正确。如何访问的路径,这种SD卡?

My app writes files to the path obtained by getExternalStorageDirectory, and I've had users ask for an option to write to their removable SD card instead. I had always assumed that getExternalStorageDirectory returned the path to the removable SD card, but this is no longer true. How do I access the path to this SD card?

推荐答案

According到源, getExternalStorageDirectory 实施返回无论是设置为外部存储的设备环境:

According to the source, getExternalStorageDirectory is implemented to return whatever is set as "external storage" in the device environment:

public static File getExternalStorageDirectory() {
    return EXTERNAL_STORAGE_DIRECTORY;
}

EXTERNAL_STORAG​​E_DIRECTORY 是:

private static final File EXTERNAL_STORAGE_DIRECTORY
        = getDirectory("EXTERNAL_STORAGE", "/sdcard");

static File getDirectory(String variableName, String defaultPath) {
    String path = System.getenv(variableName);
    return path == null ? new File(defaultPath) : new File(path);
}

在此相反, getExternalStoragePublicDirectory(字符串类型)需要这些字符串之一:

In contrast, getExternalStoragePublicDirectory(String type) requires one of these strings:

DIRECTORY_MUSIC,DIRECTORY_PODCASTS,DIRECTORY_RINGTONES,DIRECTORY_ALARMS,DIRECTORY_NOTIFICATIONS,DIRECTORY_PICTURES,DIRECTORY_MOVIES,DIRECTORY_DOWNLOADS,或DIRECTORY_DCIM。 不能为null

DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM. May not be null.

所以它并不意味着以返回在SD卡根

so it is not meant to return the sd-card root.

最后, getExternalStorageState()将返回安装在到/ mnt / SD卡/ 的文件系统。根据CommonsWare在这样的回答:Find外部SD卡地点,没有办法直接获取外部SD卡(如果它甚至存在)。

Finally, getExternalStorageState() will return the filesystem mounted in /mnt/sdcard/. According to CommonsWare in this answer: Find an external SD card location, there is no way to directly get the external sdcard (if it even exist).

另一种方法是检查 isExternalStorageRemovable()并给予了手动选项,如果它是假的。

An alternative would be to check isExternalStorageRemovable () and give a manual option if it is false.