How can I read files from usb device on android?

自古美人都是妖i 提交于 2019-12-07 05:19:04

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/41778676/how-can-i-read-files-from-usb-device-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!