Get access to USB mass storage device in android

前端 未结 2 1990
再見小時候
再見小時候 2020-12-08 10:34

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:

2条回答
  •  独厮守ぢ
    2020-12-08 11:05

    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 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);
            }
        }
    }
    

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

    
    
    
    
    

提交回复
热议问题