如何在android中访问USB路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11291882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access USB Path in android?
提问by Dinesh Lingam
I am connected android device and PC via USB cable. My Internal SD Card location Path as /mnt/sdcard
. But my External USB device path as /mnt/userdata1
. I am try to use this code to find only the Internal SD Card Path Environment.getExternalStorageDirectory()
. I am using this code to access only in the internal SD Card path. How to access the external USB Path.
我通过 USB 电缆连接了 android 设备和 PC。我的内部 SD 卡位置路径为/mnt/sdcard
. 但我的外部 USB 设备路径为/mnt/userdata1
. 我尝试使用此代码仅查找 Internal SD Card Path Environment.getExternalStorageDirectory()
。我使用此代码仅在内部 SD 卡路径中访问。如何访问外部 USB 路径。
For example Screenshot is here...
Example
In this example contains Internal Memory
, External SD card
and USB Storage
. How to find this path ( Internal Memory
, External SD card
and USB Storage
) programmatically. In this code Environment.getExternalStorageDirectory()
is viewed files from all Internal Memory
Files only. So how to access others path ( External SD card
and USB Storage
)
Please guide me with code. Thanks..
例如 Screenshot is here...
Example
在这个例子中包含Internal Memory
,External SD card
和USB Storage
。如何以编程方式找到此路径(Internal Memory
,External SD card
和USB Storage
)。在此代码中Environment.getExternalStorageDirectory()
仅查看所有Internal Memory
文件中的文件。那么如何访问其他路径(External SD card
和USB Storage
)
请用代码指导我。谢谢..
采纳答案by Aleks G
If I understand correctly, what you are calling "external" USB path is actually the mount point for your SD card on your computer. Most likely, your SD card has label userdata1
. Therefore when it's mounted on the computer, it gets /mnt/userdata1
mount point. However this is not strictly necessary and it can be any mount point at all. In fact, if you connect it to another computer, it can easily be another mount point.
如果我理解正确的话,您所说的“外部”USB 路径实际上是您计算机上 SD 卡的安装点。最有可能的是,您的 SD 卡上有标签userdata1
。因此,当它安装在计算机上时,它会获得/mnt/userdata1
安装点。然而,这并不是绝对必要的,它可以是任何挂载点。事实上,如果将它连接到另一台计算机,它很容易成为另一个挂载点。
Because this path is determined by the computer operating system, you'll need to find this path on your computer (note that this can be different every time you connect your phone to your PC, so you'll need to do it every time).
因为这个路径是由电脑操作系统决定的,所以你需要在电脑上找到这个路径(注意,每次你把手机连接到电脑上这可能会有所不同,所以你每次都需要这样做) .
From your question and path structure (/mnt/userdata1
) I'm guessing you're using linux or some other unix version. Therefore you could run mount
on your PC to see the list of the mounted devices. For example, here's the output on my mac:
从您的问题和路径结构 ( /mnt/userdata1
) 我猜您使用的是 linux 或其他一些 unix 版本。因此,您可以mount
在 PC 上运行以查看已安装设备的列表。例如,这是我的 mac 上的输出:
$ mount
/dev/disk0s2 on / (hfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
/dev/disk1s1 on /Volumes/ALEKS540 (msdos, local, nodev, nosuid, noowners)
Note the last line in the output - this is my connected android phone with the SD card mounted to the computer. On macs, the mount points are created under /Volumes
instead of /mnt
. Other than than, ALEKS540is the label of my SD card, hence it's mounted this way.
请注意输出中的最后一行 - 这是我连接的 Android 手机,SD 卡安装在计算机上。在Mac上,正在创建的挂载点/Volumes
,而不是/mnt
。除此之外,ALEKS540是我的 SD 卡的标签,因此它是这样安装的。
Internally on the phone, it's still mounted as /mnt/sdcard
.
在手机内部,它仍然安装为/mnt/sdcard
.
From the point of view of Android, there may be three storage types:
从Android的角度来看,可能存在三种存储类型:
- Internal memoryit's always mounted under
/
on the device and contains everything except the SD card and USB storage below. - SD card- this is referred to as "external storage" and is usually mounted as
/mnt/sd
, but not always.Environment.getExternalStorageDirectory()
will return the path of SD card mount point. - USB storage- this is only supported on very few devices (those that support USB host mode for external storage). This will be mounted somewhere under
/mnt
, but the exact location will vary. You will need to use Android NDK to interrogate and iterate mounted devices to find the one you're after.
- 内部存储器,它始终安装在
/
设备下方,包含除下面的 SD 卡和 USB 存储器之外的所有内容。 - SD 卡- 这被称为“外部存储”,通常安装为
/mnt/sd
,但并非总是如此。Environment.getExternalStorageDirectory()
将返回 SD 卡挂载点的路径。 - USB 存储- 这仅在极少数设备上受支持(支持 USB 主机模式用于外部存储的设备)。这将安装在 下的某个
/mnt
位置,但确切位置会有所不同。您将需要使用 Android NDK 来查询和迭代已安装的设备以找到您想要的设备。