Environment.getExternalStorageDirectory不返回的路径移动存储 [英] Environment.getExternalStorageDirectory does not return the path to the removable storage

查看:273
本文介绍了Environment.getExternalStorageDirectory不返回的路径移动存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于API 8级,看来Android已经重新定义了外部存储器是什么。通过阅读<一个href="http://developer.android.com/reference/android/os/Environment.html">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?

推荐答案

<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2_r1.1/android/os/Environment.java#Environment.getExternalStorageDirectory%28%29">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在这样的回答:<一href="http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location/5695129#5695129">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.

这篇关于Environment.getExternalStorageDirectory不返回的路径移动存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆