Java 在 android 中访问 USB 大容量存储设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24075352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:01:29  来源:igfitidea点击:

Get access to USB mass storage device in android

javaandroidfilefile-io

提问by

So I have this little cable that you plug into your phone that has a USB port on the other side where you can plug in a flash drive for example, as you can see here:

所以我有一条小电缆,你可以将它插入手机,它的另一侧有一个 USB 端口,你可以在那里插入闪存驱动器,例如,如下所示:

enter image description here

在此处输入图片说明

When I plug in a flash drive I get a notification that says:

当我插入闪存驱动器时,我收到一条通知,内容为:

USB mass storage connected

已连接 USB 大容量存储设备

When I then launch a file explorer app I can see that the drive is then located at:

然后当我启动文件资源管理器应用程序时,我可以看到驱动器位于:

/storage/UsbDriveA/

/存储/UsbDriveA/

And that's great, but I want to know how to gain access to the flash drive in my code. Getting access to the SD card is easy enough:

这很好,但我想知道如何在我的代码中访问闪存驱动器。访问 SD 卡很容易:

File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/MyFiles")
directory.mkdirs();

But how would you do this in the case of the flash drive? Thanks in advance! :)

但是在闪存驱动器的情况下,您将如何做到这一点?提前致谢!:)

采纳答案by JBA

In this example I am using the FileUtils from Apache, but event without it you will see the logic used to read a USB Flash drive:

在这个例子中,我使用了来自 Apache 的 FileUtils,但如果没有它,您将看到用于读取 USB 闪存驱动器的逻辑:

private UsbManager usbManager;
private UsbDevice clef;
ArrayList<File> images;

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

if (usbManager != null)
{
    HashMap<String,UsbDevice> deviceList = usbManager.getDeviceList();
    if (deviceList != null)
    {
        Iterator<UsbDevice> 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<File>();
            String[] imageExtensions = {"jpg","jpeg","png","gif","JPG","JPEG","PNG","GIF"};
            Iterator<File> 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);
        }
    }
}

In my manifest I have those lines, although I'm not sure they're all mandatory...

在我的清单中,我有这些行,虽然我不确定它们都是强制性的......

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

回答by ballesta25

I can't test this, not having such a cable myself, but my assumption would be that you can pass your filepath directly into the constructor which would look like:

我无法对此进行测试,我自己没有这样的电缆,但我的假设是您可以将文件路径直接传递到构造函数中,如下所示:

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

Have you tried this?

你试过这个吗?