Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

java - Get access to USB mass storage device in android

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:

enter image description here

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

USB mass storage connected

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

/storage/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:

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! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...