JAVA获取安卓系统下usb_java – 在Android中访问USB大容量存储设备

该示例展示了如何在Android设备上通过JAVA获取并读取USB大容量存储设备(如USB闪存驱动器)中的文件,特别是图像。首先获取UsbManager实例,遍历设备列表找到USB设备,然后访问其对应的文件目录,读取指定类型的文件。
摘要由CSDN通过智能技术生成

在这个例子中,我使用Apache的FileUtils,但事件没有它会看到用于读取USB闪存驱动器的逻辑:

private UsbManager usbManager;

private UsbDevice clef;

ArrayList images;

usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

clef = null;

if (usbManager != null)

{

HashMap deviceList = usbManager.getDeviceList();

if (deviceList != null)

{

Iterator deviceIterator = deviceList.values().iterator();

while (deviceIterator.hasNext()) {

clef = deviceIterator.next();

}

}

}

if (clef != null)

{

File directory = new File("/storage/UsbDriveA/");

if (directory != null) {

if (directory.canRead()) {

images = new ArrayList();

String[] imageExtensions = {"jpg","jpeg","png","gif","JPG","JPEG","PNG","GIF"};

Iterator iterateImages = FileUtils.iterateFiles(directory, imageExtensions, true);

while (iterateImages.hasNext()) {

File theImage = iterateImages.next();

if (!theImage.getName().startsWith(".", 0))

images.add(theImage);

}

// custom process / methods... not very relevant here :

imageIndex = 0;

scale = 1.0f;

countImgs = images.size();

loadImage(imageIndex);

}

}

}

在我的清单中我有这些线,虽然我不知道他们是强制性的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值