Ionic中如何通过Cordova File插件访问外置USB存储设备?

How to access an external USB storage device through the Cordova File plugin in Ionic?

我正在用 Ionic 创建一个应用程序,我想在其中添加从通过微型 USB(在 Android 设备上)或 Lightning(在iPhone)。

经过研究后;我没有找到除了 File plugin 之外已经构建的东西,它有很好的 API 用于 read/write 访问移动设备本身和安装在设备上的 sdcard,但我一直无法读取手机上连接的U盘。

目前我正在使用以下代码在 Android 设备上进行测试

import {Injectable} from '@angular/core';
import {File} from "@ionic-native/file";
@Injectable()
export class MyClassProvider {

  constructor(public fileNative: File) {}

  public findPathOfMainUSBFolder() {
    let fs = this.fileNative;
    let folders = [fs.applicationDirectory, fs.applicationStorageDirectory, fs.cacheDirectory, fs.dataDirectory, fs.externalRootDirectory, fs.externalApplicationStorageDirectory, fs.externalCacheDirectory, fs.externalDataDirectory, "file:///storage/"];

    for (let mainFolder of folders) {
      this.fileNative.listDir(mainFolder,'').then( success => {
        console.log("success ", mainFolder, "Contents: ", success);
      }, rej => {
        console.log("rej ", mainFolder, "Contents: ", rej);
      }).catch(err => {
        console.log("err ", mainFolder, "Contents: ", err);
      })
    }
  }
}

遍历所有available directories看是否会列出U盘的内容。 None 这些目录指向 to/is USB 上的根目录。

我是不是遗漏了什么,或者根本无法使用文件插件来访问 U 盘?

在深入研究文件系统并使用 File Chooser 帮助我识别 USB 上文件的完整路径后,我最终得到了以下函数,它为每个 USB 创建了一个包含一个对象的数组具有 USB 路径的已连接设备:

public findPathOfMainUSBFolder(callback) {

    let usbDevicesConnected = [];
    this.fileNative.listDir("file:///", "storage").then( success => {
      for (let entry of success) {
        if (entry.isDirectory == true && entry.name != "emulated" && entry.name != "self") {
          usbDevicesConnected.push({path: "file:///storage/", dirName: entry.name});
          console.log("USB ", entry.name, " has been added to the list");
        }
      }
      callback(usbDevicesConnected)
    }, rej => {
      console.log("rej file:///, storage ", "Contents: ", rej);
    }).catch(err => {
      console.log("err file:///, storage ", "Contents: ", err);
    })

}

可以如下调用列出USB根目录下的所有文件:

this.findPathOfMainUSBFolder( usbArray => {
  for (let usb of usbArray) {
    this.fileNative.listDir(usb.path, usb.dirName).then( success => {
      console.log("USB with ID " + usb.dirName + " has contents: ", success)
    }, rej => {
      console.log("REJ on USB with ID " + usb.dirName + " : ", rej)
    }).catch(err => {
      console.log("ERr on USB with ID " + usb.dirName + " : ", err)
    })
  }
})

这适用于 Android 手机,但我还没有在 iPhone 上测试过。

let usbDevicesConnected = [];
this.diagnostic.getExternalSdCardDetails()
    .then((details) => {
       details.forEach((USBDevice: {
          path: string,
          canWrite: boolean,
          freeSpace: number,
          type: string,
          filePath?: string
       }) => {
        if (USBDevice.canWrite && USBDevice.freeSpace > 100000) {
             usbDevicesConnected.push(USBDevice.filePath);
        }
   });
}).catch();

我发现的新方法是实施 android SAF https://github.com/yokodima/USBFilesManager