Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you detect USB drives and search for images  RSS feed

 
Nicholas Chang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My name is Nicholas and I am new to this forum, but I got a question to ask to guys? I am working on a project for university and would like to ask do any of you guys know how I can detect an usb drive and search and extract images from the drive.

I know there is loads of API out for detecting usb in java application. But, I cannot seems to find out how do you run the API. Like http://usb4java.org/

Appreciate any help

Thank you
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, a USB drive would be mounted by the OS as a file system like any other external drive, so you can use the standard I/O classes and wouldn't need any USB-specific code. Or are you saying that somehow this drive is not mounted by the OS?
 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I think you are confused between a usb drive and a usb device. As Ulf has said a usb drive will be mounted by the OS and will appear a a drive on the system. Java doesn't have built in support for USB devices but USB drives are handled directly by the OS and are presented as a mounted drive and so Java doesn't need USB support to access them. The library you referenced is for communicating with USB devices such as printers, scanners, modems etc plugged into a USB port.

If you are asking how do you spot whether a USB drive is available then you can use a low level polling approach such as this code I wrote some years ago for a windows platform or you may be able to do it via the nio2 watch service (I've not tried this approach though so you would need to research it).
 
Nicholas Chang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for your help. I will look into your code and try understand it. If java cannot detect devices, howcome there is an api out there like http://usb4java.org/. I am not sure how to call it in java if its correct.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the help of such a library, Java *can* detect and access USB devices. It's just USB drives for which there is no such need, because they're handled by the OS. Note the difference between *drive* and *device*.
 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is platform independent so can't interact with the hardware without using a native library.
The usb4java web site says: "usb4java is a Java library to access USB devices. It is based on the native libusb 1.0 library ..." ie You have to download the appropriate libusb library for the platform you are running on.

Can you explain exactly what you are trying to do and what hardware you are trying to access.
 
Nicholas Chang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to detect my IPhone or android mobile phone and hopefully with the help of that API library, it might work. I am trying to detect it and search for images.
 
Nicholas Chang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know Java is only capable of finding drives like USB drives or CD drives. But, I am creating a button in my program that search for USB devices. Not sure if I need to find an API for communicate with the desktop on a window operating systems.
 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm no expert on phones and particularly iphones but when I connect my android phone it is mounted as a drive so the images stored on it would be available via the normal file system.
 
Nicholas Chang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anything wrong with this code, because whenever i tried to run it. it is not picking up my android phone as well. Only my USB drive.

 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anything wrong with this code, because whenever i tried to run it. it is not picking up my android phone as well.

1. The code will only pick up changes ie devices plugged in once it has entered the while loop. So if your phone is plugged in before starting the program it won't register.
2. Have you checked to see if your phone is being mounted as a drive using your platform's file explorer.
 
Nicholas Chang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep it is mounted. How can i make it so that if the device is plugged in already, it will register it. Do, i need a drive listener like urs.
 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 15 records the current state of the system ie which drives are there. Line 28 checks to see if that state is changed. If you want the code to do other than just print out a message when a drive is plugged in you need to rewrite it.
But unless the program is hard coded with which fixed hard drives your system has it can't know which ones are currently plugged in but removable and even then if you'd plugged in your phone and a usb drive it wouldn't know which of those two was your phone. You could however make a guess at which was the phone by looking at which directories were on the drive as I'm guessing the phone is likely to have a specific set of named directories.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!