[英]Environment.getExternalStorageDirectory does not return the path to the removable storage
As of API level 8, it seems Android has redefined what "external" storage is. 从API级别8开始,Android似乎重新定义了“外部”存储。 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." 通过http://developer.android.com/reference/android/os/Environment.html阅读,附在getExternalStorageDirectory
的文档中,我看到评论:“不要在这里混淆'外部'这个词。这个目录可以最好被认为是媒体/共享存储......在具有多个“外部”存储目录的设备中......,此目录代表用户将与之交互的“主要”外部存储。“
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. 我的应用程序将文件写入getExternalStorageDirectory
获取的路径,我已经让用户要求选择写入他们的可移动SD卡。 I had always assumed that getExternalStorageDirectory
returned the path to the removable SD card, but this is no longer true. 我一直认为getExternalStorageDirectory
返回了可移动SD卡的路径,但这不再是真的。 How do I access the path to this SD card? 如何访问此SD卡的路径?
According to the source , getExternalStorageDirectory
is implemented to return whatever is set as "external storage" in the device environment: 根据源代码 ,实现了getExternalStorageDirectory
以返回设备环境中设置为“外部存储”的内容:
public static File getExternalStorageDirectory() {
return EXTERNAL_STORAGE_DIRECTORY;
}
and EXTERNAL_STORAGE_DIRECTORY
is: 和EXTERNAL_STORAGE_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);
}
In contrast, getExternalStoragePublicDirectory(String type)
requires one of these strings: 相反, getExternalStoragePublicDirectory(String type)
需要以下字符串之一:
DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM. DIRECTORY_MUSIC,DIRECTORY_PODCASTS,DIRECTORY_RINGTONES,DIRECTORY_ALARMS,DIRECTORY_NOTIFICATIONS,DIRECTORY_PICTURES,DIRECTORY_MOVIES,DIRECTORY_DOWNLOADS或DIRECTORY_DCIM。 May not be null . 不能为空 。
so it is not meant to return the sd-card root. 所以它并不意味着返回SD卡根。
Finally, getExternalStorageState()
will return the filesystem mounted in /mnt/sdcard/
. 最后, getExternalStorageState()
将返回挂载在/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). 根据CommonsWare在这个答案: 找到外部SD卡位置 ,没有办法直接获取外部SD卡(如果它甚至存在)。
An alternative would be to check isExternalStorageRemovable ()
and give a manual option if it is false. 另一种方法是检查isExternalStorageRemovable ()
并在其为false时给出手动选项。
For API 17 I get the following returns: 对于API 17,我得到以下回报:
Environment.getExternalStoragePublicDirectory(Environment.MEDIA_MOUNTED)
returns:-------> /storage/sdcard0/mounted
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
returns:-------> /storage/sdcard0/DCIM
Environment.getExternalStoragePublicDirectory(Environment.MEDIA_SHARED)
returns:-------> /storage/sdcard0/shared
Environment.MEDIA_MOUNTED
returns:-------> mounted
Environment.getExternalStorageDirectory()
returned:-------> /storage/sdcard0
All return location of internal phone memory. 内部手机内存的所有返回位置。
To be sure you are accessing the sd card you can use: 为了确保您访问SD卡,您可以使用:
System.getenv("SECONDARY_STORAGE") System.getenv( “SECONDARY_STORAGE”)
however - please keep in mind that even if you set all the correct permissions in the manifest - The only place 3rd party apps are allowed to write on your external card are "their own directories" (ie /sdcard/Android/data/) 但请记住,即使您在清单中设置了所有正确的权限 - 允许第三方应用程序在外部卡上写入的唯一位置是“他们自己的目录”(即/ sdcard / Android / data /)
Try to write to anywhere else and you will get exception: EACCES (Permission denied) 尝试写到其他任何地方,你会得到例外:EACCES(权限被拒绝)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.