问题
I'm developing using a Sony Internet TV as a development device, which has 4 USB ports. All or no ports can be used with external hard drives. How can I select and access external memory vs internal memory on the device.
I'm able to access the SD card, or at least what the TV labels as the SD card using the following:
Environment.getExternalStorageDirectory();
But I can't seem to figure out how to find external USB devices. I've attempted to go down the path of the UsbManager class, but it seems as though there should be an easier and more general way. I just want to be able to see mass storage devices and don't want to have to clean through vendor id's etc. But maybe I'm missing something there.
This should be possible as I'm looking for the same functionality found by the Media Player app when opening the menu and selecting "Select device".
I only need read access to the drives, but read/write could be useful.
Thanks in advance.
回答1:
In Google TV v2, there were some changes regarding USB storage support.
The major changes for USB drives are:
1.USB drives will be mounted /mnt/media/usb.XXXXX instead of /sdcard.
(XXXXX is decided by the ID(volume id : vfat or exfat, uuid : ntfs) of USB volume.)
2.GTV supports multiple USB drives. Users can plug multiple USB drives.
MediaProvider indexes all the media files in all the plugged USB volumes. So, applications can query against MediaProvider in order to retrieve all the media files.
Sample code:
Cursor cursor = getContentResolver().query(
Images.Media.EXTERNAL_CONTENT_URI,
new String[] {
Images.Media._ID,
Images.Media.DATA, // file path
},
null, null, null);
How can application detect when the drive has synced or ejected?:
You can register BroadcastReceiver in order to receive
Intent.ACTION_MEDIA_SCANNER_FINISHED and Intent.ACTION_MEDIA_EJECT.
回答2:
So intuition says to use the USB accessories api. However, this is only avaible to android 3.1 and up and if I remember correctly, google tv adhers to the 3.0 API. That said, from the documentation:
com.android.future.usb: To support USB accessory mode in Android 2.3.4, the Google APIs add-on library includes the backported USB accessory APIs and they are contained in this namespace.
So checkout the Google APIs add-on library to access the usb stuff.
回答3:
But I can't seem to figure out how to find external USB devices.
There is no official support in the Android SDK for external USB drives. I see nothing in the Google TV developer documentation to suggest that they have added anything in this area.
回答4:
You can't access the usb devices with any getExternalXXX()
, and by going through MediaProvider you will have a flat collection (the directory structure is lost) of the only files classified as media (e.g. no .txt).
I developed libmedia for this need and some others. Tested on a Sony NSZ-GS7.
Sample code:
VolumeManager volumeManager = new VolumeManager();
List<Volume> volumes = volumeManager.getVolumes();
for (Volume volume: volumes) {
String label = volume.getLabel();
File rootFolder = volume.getRoot();
File[] files = rootFolder.listFiles();
for (File file: files) {
String filename = file.getName();
}
}
来源:https://stackoverflow.com/questions/8115317/how-can-you-access-external-memory-on-google-tv