How can you access external memory on Google TV

dzimney picture dzimney · Nov 13, 2011 · Viewed 12.5k times · Source

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.

Answer

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.