传输文件USB大容量存储OTG [英] Transfer Files USB Mass Storage OTG

查看:135
本文介绍了传输文件USB大容量存储OTG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到Android设备的外部大容量存储设备.根目录中有几个.BIN文件,我需要读入我的应用程序.我能够连接到设备并使用UsbDeviceConnection接收USB许可.

I have an external mass storage device connected to an Android device. There are several .BIN files in the root directory that I need to read into my app. I am able to connect to the device and receive usb permission using UsbDeviceConnection.

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
    String output = outputTV.getText().toString();
    String action = intent.getAction();
    outputTV.setText(outputTV.getText() + "\n" + " ACTION: " + action);

    if (ACTION_USB_PERMISSION.equals(action)) {
        synchronized (this) {
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

            if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                if (device != null) {
                    if (device.getInterfaceCount() > 0) {
                        usbIntf = device.getInterface(0);
                        usbEndIn = usbIntf.getEndpoint(0);
                        usbEndOut = usbIntf.getEndpoint(1);

                        usbConnection = mUsbManager.openDevice(device);
                        usbConnection.claimInterface(usbIntf, true);

                        byte[] buffer = new byte[usbEndIn.getMaxPacketSize()];
                        StringBuilder str = new StringBuilder();
                        int retVal = usbConnection.bulkTransfer(usbEndIn, buffer, usbEndIn.getMaxPacketSize(), 2000);

                        for (int i = 2; i < buffer.length; i++) {
                            if (buffer[i] != 0) {
                                str.append((char) buffer[i]);
                            }}}}}}}}};

连接后,我可以看到我具有一个带有2个批量传输端点(一个进出一个)的大容量存储接口.

Once I'm connected I am able to see that I have one Mass Storage interface with 2 Bulk Transfer endpoints (one in, one out).

当使用usbConnection.bulkTransfer时,我收到-1并返回一个空缓冲区.因此,我在接收数据时遇到了麻烦.一旦越过障碍,数据将是什么样?如何继续从设备中提取所有文件?有没有一种方法可以将文件逐个文件传输到我的应用程序?有没有更好的方法来解决这个问题?

When using usbConnection.bulkTransfer I receive -1 back and an empty buffer. So, I'm having trouble receiving data. Once past that hurdle, what will the data look like? How do I continue to pull all of the files from the device? Is there a way to transfer the data file by file to my app? Is there an all together better way of approaching this?

推荐答案

好吧,我最终走了一条完全不同的路线.我现在正在使用标准的java.io.File并通过目录结构进行工作.我的代码尚未完善,但这是要点...

Well, I ended up going a totally different route with this. I am now using standard java.io.File and working my way through the directory structure. My code is not yet refined, but here is the gist of it...

private void setupFileIO() {
    File storageDir = new File("/storage");
    File files[] = storageDir.listFiles();

    File directory = null;
    if (files != null && files.length > 0) {
        for (File f : files) {
            if (f.isDirectory() && f.getPath().toLowerCase().contains("usb") && f.canRead()) {
                directory = f;
                break;
            }
        }
    }
    if (directory != null && directory.canRead()) {
        File file[] = directory.listFiles();
        if (file != null && file.length > 0) {
            for (int i = 0; i < file.length; i++) {
                String filePath = file[i].getPath();
                String extension = Files.getFileExtension(filePath);
                if (file[i].isFile() && file[i].canRead() && extension.equalsIgnoreCase("bin")) {
                    try {
                        byte[] r = Files.toByteArray(file[i]);
                        String result = BaseEncoding.base16().encode(r);
                        // do something with result
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

这篇关于传输文件USB大容量存储OTG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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