【问题标题】:Send a simple picture through USB Host on AndroidAndroid上通过USB Host发送简单图片
【发布时间】:2017-01-05 17:06:01
【问题描述】:
【问题讨论】:
标签:
android
android-usb
usb-hostcontroller
【解决方案1】:
我不确定您到底卡在哪里,但您发布的链接似乎包含所需的所有信息。
一旦您持有您的UsbDevice,您需要请求通信权限(请参阅获取与设备通信的权限)。然后您可以使用以下代码传输数据:
private Byte[] bytes; //Convert your jpeg into a byte array and load it in this variable.
private static int TIMEOUT = 0;
private boolean forceClaim = true;
...
UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = mUsbManager.openDevice(device); //this opens the connection
connection.claimInterface(intf, forceClaim);
connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT); //this actually sends the bytes to the other device.
另一方面,您需要将字节数组转换回 jpeg
See this code for a full sample.