I am currently making an app that needs to be able to read from and write to a USB flash drive connected via a USB OTG adapter. Is there an easy way to access this storage via standard Java.io.File APIs? This app is only going to be run on a rooted Motorola Xoom running Android 4.2.2. Any suggestions would be appreciated.
USB Drives get mounted to your device just like an SDCard does essentially*.
The mount path usually resides at:
/storage/usb0/
I have not used this on many devices other then my Droid running CyanogenMod, your device may very. You can smiply use a file manager to explore this path. The directories will still exist even if there is no mount path, so you will be able to determine the path.
Related
I am learning Android. I have the latest, as of today, Android Studio and SDK.
I have gone through the tutorial on "Saving Data" at developer.android.com. I have perused StackOverflow for a couple hours and can't seem to find precisely what I am looking for; so here goes.
My app will have a need to read and write from a directory on the Android device. This directory needs to be accessible when connected to my PC via Windows Explorer. I must admit I am confused by getExternalFilesDir(), getExternalStoragePublicDirectory() and all the other getFilesDir() methods.
Question 1: How do I create, access, read/write a simple text file to such a directory. It would be fine if this directory goes away if the user uninstalls the App. But the directory must be accessible to my App on the phone, and to Windows Explorer when connected to my PC via USB cable. Those are the only requirements.
Question 2: In trying to figure this out, I am using an AVD virtual device. When querying getExternalStorageState(), it says "Removed". Can I access such a directory when debugging using the AVD virtual device; and if so, what am I missing? I have the "WRITE_EXTERNAL_STORAGE" permission in my manifest file. I have also set my AVD with the "SD Card" set to "Studio-managed" and "200 MB".
Thanks.
Oh my. It turns out I was doing this correctly, but had updated my code and settings without restarting the AVD.
Once I restarted my AVD, the external storage (SD Card) showed as Mounted and I was able to read/write to the "Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);" directory.
I believe that this directory will be accessible from Windows Explorer when the device is connected to my PC.
Is there a way to write a driver for android phone, so that when i plug it in the computer it's going to install it with the default usb drivers? I want to make a application that needs a driver to be installed on the computer to use it.
You could have your Android App download or contain the installer for your driver and then copy it to the SD card storage area. There is no way to get the installer to autorun because Android connects either with MPT or PTP. These can be used to access a file from the SD card, but it does not mount as an ordinary storage device. You will need to have instructions in the app to tell the user how to access it.
Honestly, it will probably be easier to have them download it separately and install it.
It may seem strange, but I wonder is an android app able to make the SD Card inside the device windows bootable? I thought of making the SD bootable and write ultimatebootcd to it. I did it through windows, but it would be so great if android can do it!
To make the SD card bootable, you have to modify the boot sector of the card. This can not be done from a standard Android application. If the Android device is rooted, it can almost certainly be done from a native Linux program, or an NDK program.
If the device is not rooted, you can probably not do what you want. As Chris Stratton commented, if the card is already made bootable, you could probably then change for instance what OS is booted up and how.
Say, there is startup code which loads and boots an OS, then you can change the configuration file for the bootloader. A starter: http://www.syslinux.org/wiki/index.php/Boot_an_Iso_image
I used Rufus and installed a syslynux on the SdCard from a PC running window 10.then I put the card back into the Moto phone. It's receiving files that I have moved from the internal storage to the card how ever I have not a chance to find out whether they Will open up In the PC or not.
I am developing an application that has the following requirement:
When the device is connected to a computer via USB, the user should be
able to use Windows Explorer to drag files into and out of the
application directory.
The application will consume some of these files and produces others
which should be able to be copied back to the computer.
The target device does not have an external storage device (for
example an SD Card).
The directories that the files will be stored in should be accessible
whilst the application is running.
I've looked into the data storage page in the android documentation, but none of the methods stated there seem to do what I require.
Alternatively, is there a way to select the computer when the application is running and 'send' files via USB to the computer? How about for 'receiving' files?
What is the best way to fulfill this requirement?
When the device is connected to a computer via USB, the user should be able to use Windows Explorer to drag files into and out of the application directory.
If "the application directory" refers to a spot on external storage, this is possible.
The application will consume some of these files and produces others which should be able to be copied back to the computer.
If this is supposed to happen simultaneously, the device must be running Android 3.x or higher. Android 1.x and 2.x did not support simultaneous access to external storage.
The target device does not have an external storage device (for example an SD Card).
It needs to have something designated as "external storage". That does not have to be removable storage. On Android 3.x and 4.x, it is usually just a part of on-board flash that was designated to be accessible as external storage. In Android terms, "external storage" means "can be accessed by a host computer using USB and by all applications on the device as well".
The directories that the files will be stored in should be accessible whilst the application is running.
Again, this requires Android 3.x or higher. If this is a custom device, that most likely means you are going to need to use the recently-released Android 4.0 source code.
Alternatively, is there a way to select the computer when the application is running and 'send' files via USB to the computer? How about for 'receiving' files?
No and no, respectively.
What your going to want to do is use Environment.getExternalStorageDirectory. Even if there is no sd card, most android devices partition a part of the on board memory to be used as external storage.
I have a Motorola flipout with Android 2.1. and the USB my driver is the following:
http://developer.motorola.com/docstools/USB_Drivers/Handset_USB_Driver_64/
Whenever I connect the USB cord to the cellphone I can access the SD card from the computer and I no longer can access it from the cellphone. That means that any software on the phone that needs to access the SD card can't do so, therefore I can't debug anything that needs file access. Is there any way to connect the USB cord yet at the same time not mount the SD card file system onto the computer filesystem?
The SD card can only be mounted by one device at a time. It sounds like the default setting upon USB connection is to basically mount the SD card to your computer so it can browse/edit the files. Use blindstuff's method to disable USB storage mode. your phone will still be connected via USB allowing you to use adb to debug applications.
Blindstuff called it. The reason you cannot is because the SD card is in use by the computer (depending on how you've connected of course). While the SD card is in use by the computer, it's not available to the device.
I strongly suspect that's an artifact of SD cards, not Android (or iPhone, or anything else).