Java Android从USB主机获取文件

时间:2019-02-19 18:56:20

标签: java android

我尝试从USB大容量存储中获取文件。我的存储设备通过USB连接,但我尝试这样做,但是无法正常工作

设备有两个USB端口,其中一个已连接并读取名称(这是GPS),第二个端口已连接USB存储设备,我想读取其中的文件。我只能在设备的一个存储上执行以下读取:

Map<String, File> externalLocations = ExternalStorage.getAllStorageLocations();

File externalSdCard = externalLocations.get(ExternalStorage.EXTERNAL_SD_CARD);
if (externalSdCard != null && externalSdCard.exists() && externalSdCard.listFiles() != null) {
    for (File f : externalSdCard.listFiles()) {
        if ("key.xml".equalsIgnoreCase(f.getName())) {
            String path = f.getPath().replace(".xml", "_DUMP.xml");
            File file11 = new File(path);
            if (!file11.exists()) {
                moveFile(file11, f);
            }
            return f;
        }
    }
}
And I think I should do this to read a file :

UsbManager usbManager = ((UsbManager)(GetSystemService(Context.UsbService)));
var deviceList = usbManager.DeviceList;
IEnumerable<UsbDevice> deviceIterator = deviceList.Values.AsEnumerable();
if ((deviceIterator.Count() > 0)) {
    UsbDevice device = deviceIterator.ElementAt(0);
    usbManager.RequestPermission(device, mPermissionIntent);
    if (usbManager.HasPermission(device)) {
        if (usbManager.HasPermission(device)) {
            System.Diagnostics.Debug.WriteLine("HasPermission");
            UsbDeviceConnection connection = usbManager.OpenDevice(device);
            if ((connection != null)) {
                System.Diagnostics.Debug.WriteLine(" connection is not null");
                var model = device.DeviceName;
                string inline = null;
                BufferedReader reader = null;
                reader = new BufferedReader(new FileReader("/mnt/media_rw/C227-875D/test/hello.txt"));
                System.Diagnostics.Debug.WriteLine("no exception occured");
                while ((reader.ReadLine() != null)) {
                    System.Diagnostics.Debug.WriteLine(inlne);
                }

            }

        }

但是我可以同时打开两个端口吗?

0 个答案:

没有答案