我知道如何做以下工作:
最后,我有了一个UsbDevice,并且我有读写它的权限。从这里怎么走?
我可以打开设备并获得一个FileDescriptor,如下所示:
UsbManager manager = (UsbManager) activity.getSystemService(Context.USB_SERVICE);
UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = manager.openDevice(device);
boolean interfaceClaimed = connection.claimInterface(intf, true);
int fileDescriptor = connection.getFileDescriptor();我现在如何使用这个FileDescriptor?或者我如何访问USB设备上的文件?
编辑
UsbDeviceConnection,只需尝试找到usb设备路径。但是,您仍然有问题要区分多个usb设备,并且不知道哪条路径属于哪个设备.发布于 2016-02-05 13:12:17
因为我和您一样困惑,所以我会选择使用ls命令的路线。
代码示例:
try {
Process process = Runtime.getRuntime().exec("ls /storage/UsbDriveA");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String listOfFiles = "";
String line;
while ((line = buffer.readLine()) != null) {
listOfFiles += line;
}
} catch (InterruptedException e) {
e.printStackTrace();
}如果挂载点不正确,ls将返回no such file or directory。
以下是制造商使用的许多安装点的列表:
/storage/UsbDriveA (all Samsung devices)
/storage/USBstorage1 (LG G4, V10, G3, G2, other LG devices)
/storage/usbdisk (Moto Maxx, Turbo 2, Moto X Pure, other Motorola devices)
/storage/usbotg (Sony Xperia devices, Lenovo Tabs)
/storage/UDiskA (Oppo devices)
/storage/usb-storage (Acer Iconia Tabs)
/storage/usbcard (Dell Venue -- Vanilla Android 4.3 tablet)
/storage/usb (HTC One M7, and some Vanilla Android devices)(根据我收集的设备和其他3个人的资料,并快速前往贝斯台测试最新的旗舰。据我所知,我所缺少的唯一流行的设备是基于中国的Hawuei/Xioami设备,这些设备在英国越来越受欢迎,但它们很可能使用上述其中一种。)
要找到正确的挂载点,您需要侦听no such file or directory并用下一个挂载点循环/重新执行exec()语句。
一旦全部完成,您就可以轻松地使用cat /storage/.../file.txt读取文件,并使用重定向:echo test > /storage/.../file.txt编写文件。
希望这能有所帮助
https://stackoverflow.com/questions/35089466
复制相似问题