查找可移动 SD 卡的位置 [英] Find location of a removable SD card

查看:28
本文介绍了查找可移动 SD 卡的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

请不要与外部存储混淆

Please, do not be confused with External Storage.

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

Environment.getExternalStorageState() returns the path to the internal SD mount point, such as /mnt/sdcard. But the question is about the external SD. How do I get a path like /mnt/sdcard/external_sd (it may differ from device to device)?

我想我将以按文件系统名称过滤 mount 命令的输出结束.但我不确定这种方式是否足够健壮.

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

推荐答案

Environment.getExternalStorageState() 返回内部 SD 挂载点的路径,如/mnt/sdcard"

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

不,Environment.getExternalStorageDirectory() 指的是设备制造商认为是外部存储"的任何东西.在某些设备上,这是可移动媒体,如 SD 卡.在某些设备上,这是设备上闪存的一部分.这里,外部存储"指的是表示安装在主机上时可通过 USB 大容量存储模式访问的东西",至少对于 Android 1.x 和 2.x.

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.

但问题是关于外部 SD.如何获得像/mnt/sdcard/external_sd"这样的路径(它可能因设备而异)?

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"的概念,除了外部存储,如上所述.

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

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

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.

更新

最近需要注意的两件事:

Two recent things of note:

首先,在 Android 4.4+ 上,除了 getExternalFilesDirs()getExternalCacheDirs().请参阅 Dave Smith 对此的出色分析,特别是如果您想要低- 级别的详细信息.

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.

其次,为了避免有人质疑可移动媒体访问是否是 Android SDK 的一部分,这里是 Dianne Hackborn 的评估:

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

...请记住:在 Android 4.4 之前,Android 官方平台所有都不支持 SD 卡,除了两种特殊情况:外部存储是 SD 卡的老派存储布局(今天该平台仍然支持),并且在 Android 3.0 中添加了一个小功能,它可以扫描额外的 SD 卡并将它们添加到媒体提供商,并为应用程序提供对其文件的只读访问(这在今天的平台).

...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 是该平台的第一个版本,它实际上允许应用程序使用 SD 卡进行存储.在此之前对它们的任何访问都是通过不受支持的私有 API 进行的.我们现在在平台中有一个非常丰富的 API,它允许应用程序以受支持的方式使用 SD 卡,比以前更好的方式:他们可以免费使用他们的应用程序特定的存储区域,而无需任何应用程序中的权限,并且可以访问 SD 卡上的任何其他文件,只要它们通过文件选择器,同样不需要任何特殊权限.

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.

这篇关于查找可移动 SD 卡的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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