android - 查找可移动 SD 卡的位置

标签 android external sd-card

有没有通用的方法来查找外部 SD 卡的位置?

请不要与 External Storage 混淆.

Environment.getExternalStorageState()返回内部 SD 挂载点的路径,例如 /mnt/sdcard。但问题是关于外部 SD 的。如何获得类似 /mnt/sdcard/external_sd 的路径(可能因设备而异)?

我想我将通过文件系统名称过滤 mount 命令的输出来结束。但我不确定这种方式是否足够强大。

最佳答案

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

不,Environment.getExternalStorageDirectory() 指的是设备制造商认为是“外部存储”的任何内容。在某些设备上,这是可移动媒体,例如 SD 卡。在某些设备上,这是设备上闪存的一部分。在这里,“外部存储”是指“安装在主机上时可通过 USB 大容量存储模式访问的东西”,至少对于 Android 1.x 和 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 没有“外部 SD”的概念。

如果设备制造商选择将外部存储设备作为板载闪存并且还具有 SD 卡,则您需要联系该制造商以确定您是否可以使用 SD 卡(不保证)以及使用它的规则,例如使用什么路径。


更新

最近要注意的两件事:

首先,在 Android 4.4+ 上,您没有对可移动媒体(例如“外部 SD”)的写入权限,但该媒体上可能由 getExternalFilesDirs() 返回的任何位置除外和 getExternalCacheDirs()。见 Dave Smith's excellent analysis尤其是如果您想要低级别的详细信息。

第二,免得有人质疑可移动媒体访问是否属于 Android SDK 的一部分,这里是 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.

关于android - 查找可移动 SD 卡的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694933/

相关文章:

android - 指示自定义选项卡不建议身份验证存储

android - 删除 ImageView 的阴影

webpack - 在 webpack 运行时加载 json 文件

android - 如何在android中的sdcard中创建文件夹

android - 从 Fabric Crashlytics 更新后,Firebase Crashlytics 在运行时崩溃

java - webView 无法解析 & 无法从 Activity 类型对非静态方法 findViewById(int) 进行静态引用

javascript - Thymeleaf 外部 javascript 文件与 html 文件共享模块属性

javascript - QML - 导入外部 JavaScript 文件

android - 创建的文件夹在文件资源管理器中不可见。

android - 防止用户删除sdcard中的文件夹或文件