如何从Android上的USB设备读取文件? [英] How can I read files from usb device on android?

查看:1154
本文介绍了如何从Android上的USB设备读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过连接的USB设备(通过Android TV上的OTG或USB端口)创建类似文件资源管理器之类的东西. 我需要的是一条类似于"/storage/sda4"和设备标识符的路径,然后我可以通过simle android类File使用设备.听起来很简单,但是我找不到任何相关信息,但是所有文件浏览器都可以做到这一点(例如ESExplorer). 好的,我找到了一种简单的方法来获取所有已连接的标识符为

的USB设备

UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
usbManager.getDeviceList();

但是如何获取有关路径的信息? deviceName包含类似"/dev/bus/usb/00x"的内容,但它无济于事,我需要简单的模拟android路径("/storage/sda4").此页面 https://developer.android.com/guide/topics/connectivity/usb/host.html 告诉我需要获取UsbInterfaces并使UsbConnection进行批量传输和其他废话,我完成了所有操作,但是没有找到设备的路径或有关usb文件列表的任何其他信息.

好吧,我找到了另一种获取(不需要许可!)获取所有已连接设备的路径的方法

StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Method getVolumeListMethod = StorageManager.class.getDeclaredMethod("getVolumeList");
Object[] storageVolumeList = (Object[]) getVolumeListMethod.invoke(storageManager);

它可以工作,但是我需要标识一个设备(因为我想缓存不同的USB存储设备的文件),但是我只能从卷对象中获得mStorageId,mDescriptionId,mPrimary,mRemovable,mEmulated,mMtpReserveSpace,mAllowMassStorage,mMaxFileSize ,mOwner,mUuid,mUserLabel,mState,mSubSystem. 这些都不能识别设备:mDescriptionId和mStorageId是 USB端口唯一,mUuid为空,mUserLabel不是唯一.

Environment.getExternalFilesDirs()将无济于事,它不提供任何设备ID,并且只能在一个设备上使用.

我在这里找到了类似的问题,但它没有正确的答案 Android列表文件来自USB驱动器.

那么,是否存在获取带有路径和标识符的USB设备列表的简单方法?

解决方案

我所需要的只是一个类似"/storage/sda4"和设备标识符的路径,然后我可以通过android类File来使用设备

否,因为您没有任意访问权限到Android 4.4及更高版本上的可移动存储设备(包括USB OTG驱动器).

所有文件浏览器都可以做到(例如ESExplorer)

预安装的文件资源管理器"应用程序可能具有设备制造商或自定义ROM开发人员授予的其他权利.否则,他们也不能任意访问可移动存储.

是获取具有路径和标识符的USB设备列表的简单方法吗?

不带文件系统路径,不可以. StorageManager上的getStorageVolumes()将为您提供存储卷列表,其中包括外部存储和可移动存储.然后,您可以使用StorageVolume上的createAccessIntent()来请求用户许可使用该卷.如果他们授予许可,您会得到一个Uri回复:

  • 用作卷的临时标识符(即,如果用户弹出介质,它将不再可用作标识符,但是在此之前,可以将一个卷与另一个卷区分开),

  • 让您使用该卷的一部分内容,尽管使用的是Uri值,而不是文件系统路径

I'm trying to create something like file explorer through connected usb devices(via OTG or usb ports on android TV). All I need for this is a path something like "/storage/sda4" and device identifier, and then I can work with device through simle android class File. Is sounds simple but I can't find any info about this, but all file explorers can do it (for example ESExplorer). Ok, I find a simple way to get all connected usb devices with identifier

UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
usbManager.getDeviceList();

but how can I get an info about path? deviceName contains something like this "/dev/bus/usb/00x" but it can't help me, I need simple emulated android path ("/storage/sda4"). This page https://developer.android.com/guide/topics/connectivity/usb/host.html tells that I need to get UsbInterfaces and make UsbConnection to bulk transfer and other bullshit, I done it all but didn't find path to device or any other info about usb file list.

Ok, I find another way to get (that don't requires permission!) to get path to all connected devices

StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Method getVolumeListMethod = StorageManager.class.getDeclaredMethod("getVolumeList");
Object[] storageVolumeList = (Object[]) getVolumeListMethod.invoke(storageManager);

and it works but I need to identify a device(because I want to cache files of different usb storages) but all that I can get from volume object is mStorageId, mDescriptionId, mPrimary, mRemovable, mEmulated, mMtpReserveSpace, mAllowMassStorage, mMaxFileSize, mOwner, mUuid, mUserLabel, mState, mSubSystem. None of this can not identify the device: mDescriptionId and mStorageId are unique fot usb port, mUuid is null, mUserLabel is not unique.

Environment.getExternalFilesDirs() won't help, it don't provide any device id and works only with one device.

I find a similar question here, but it has no right answer Android list files from USB Drive.

Well, is a simple way to get list of usb devices with path and identifier exists?

解决方案

All I need for this is a path something like "/storage/sda4" and device identifier, and then I can work with device through simle android class File

No, because you do not have arbitrary access to removable storage, including USB OTG drives, on Android 4.4+.

all file explorers can do it (for example ESExplorer)

Pre-installed "file explorer" apps may have additional rights granted to them by the device manufacturer or custom ROM developer. Otherwise, they too do not have arbitrary access to removable storage.

is a simple way to get list of usb devices with path and identifier exists?

Not with a filesystem path, no. getStorageVolumes() on StorageManager will give you a list of storage volumes, which includes external storage and removable storage. You can then use createAccessIntent() on StorageVolume to ask the user for permission to work with the volume. If they grant permission, you get a Uri back that:

  • Serves as a temporary identifier for the volume (i.e., will no longer be usable as an identifier if the user ejects the media, but until then, can distinguish one volume from another), and

  • Lets you work with a portion of the contents of that volume, though using Uri values, not filesystem paths

这篇关于如何从Android上的USB设备读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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