找到外部SD卡位置位置、SD

2023-09-11 23:52:43 作者:Rangers(流浪者)

有一种普遍的方式找到一个外置SD卡的位置?

请不要混淆外部存储。 Environment.getExternalStorageState()返回路径内部SD安装,如到/ mnt / SD卡点。但问题是关于外部SD。如何得到到/ mnt / SD卡/ external_sd的路径(它可能会有所不同从设备到设备)?

我想我会用安装命令按文件系统名称过滤结束。但我不知道这样是足够强大。

解决方案   

Environment.getExternalStorageState()返回路径内部SD安装,如到/ mnt / SD卡

手机上能找到SD卡的位置和里面的东西,但电脑上找不到

没有, Environment.getExternalStorageDirectory()指的是被认为是外部存储无论设备制造商。在某些设备上,这是可移动媒体,如SD卡。在某些设备上,这是基于设备的闪存的一部分。在这里,外部存储是指通过USB Mass Storage模式中的东西安装在主机上时,至少对于Android 1.x和2.x。

  

但问题是关于外部SD。如何得到到/ mnt / SD卡/ external_sd的路径(它可能会有所不同从设备到设备)?

机器人具有外部SD的概念,除了外部存储,如上所述。

如果设备制造商已选择具有外部存储上板载闪存,也有一个SD卡,则需要联系该制造商,以确定您是否可以使用SD卡(不保证),什么规则是使用它,例如使用什么路径它

更新

最近的两个值得注意的事情:

首先,在Android 4.4+,你没有写访问可移动媒体(如外置SD),除了可能由 getExternalFilesDirs返回该介质的任何位置() getExternalCacheDirs()。请参阅此戴维·史密斯的出色分析的,特别是如果你想低层次的细节。

二,免得有人狡辩可移动媒体的访问是否以其他方式在Android SDK的一部分,这里的戴安娜Hackborn的评估:

  

...记住:直到Android 4.4系统,官方Android平台不支持SD卡的的所有的除了两个特殊情况:老同学的存储布局,其中外部存储SD卡(这是由平台今天仍然支持),和一个小功能添加到Android 3.0哪里会扫描更多的SD卡,并将其添加到媒体提供商,并给应用程序,以他们的文件只读访问(这也仍支持该平台今天)。

     

Android 4.4系统的平台,这实际上已经允许的应用程序使用SD卡进行存储的第一个版本。在此之前,他们的任何访问是通过私人的,不支持的API。我们现在在它允许应用程序使用的SD卡在支持的方式,以更好的方式比他们已经能够前的平台上相当丰富的API:他们可以免费使用他们的应用程序特定的存储区域,而不需要任何在应用程序的权限,只要他们去通过文件选择器可以访问SD卡上的任何其他文件,再无需任何特殊权限。

Is there an universal way to find the location of an external SD card?

Please, do not be confused with External Storage. Environment.getExternalStorageState() returns path to internal SD mount point like "/mnt/sdcard". But the question is about external SD. How to get a path like "/mnt/sdcard/external_sd" (it may differ from device to device)?

I guess I will end with filtering of mount command by filesystem name. But I'm not sure this way is robust enough.

解决方案

Environment.getExternalStorageState() returns path to internal SD mount point like "/mnt/sdcard"

No, Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be "external storage". On some devices, this is removable media, like an SD card. On some devices, this is a portion of on-device flash. Here, "external storage" means "the stuff accessible via USB Mass Storage mode when mounted on a host machine", at least for Android 1.x and 2.x.

But the question is about external SD. How to get a path like "/mnt/sdcard/external_sd" (it may differ from device to device)?

Android has no concept of "external SD", aside from external storage, as described above.

If a device manufacturer has elected to have external storage be on-board flash and also has an SD card, you will need to contact that manufacturer to determine whether or not you can use the SD card (not guaranteed) and what the rules are for using it, such as what path to use for it.

UPDATE

Two recent things of note:

First, on Android 4.4+, you do not have write access to removable media (e.g., "external SD"), except for any locations on that media that might be returned by getExternalFilesDirs() and getExternalCacheDirs(). See Dave Smith's excellent analysis of this, particularly if you want the low-level details.

Second, lest anyone quibble on whether or not removable media access is otherwise part of the Android SDK, here is Dianne Hackborn's assessment:

...keep in mind: until Android 4.4, the official Android platform has not supported SD cards at all except for two special cases: the old school storage layout where external storage is an SD card (which is still supported by the platform today), and a small feature added to Android 3.0 where it would scan additional SD cards and add them to the media provider and give apps read-only access to their files (which is also still supported in the platform today).

Android 4.4 is the first release of the platform that has actually allowed applications to use SD cards for storage. Any access to them prior to that was through private, unsupported APIs. We now have a quite rich API in the platform that allows applications to make use of SD cards in a supported way, in better ways than they have been able to before: they can make free use of their app-specific storage area without requiring any permissions in the app, and can access any other files on the SD card as long as they go through the file picker, again without needing any special permissions.