5

I am developing an application for google tv and I would like to read an image from a USB Flash Drive connected to the google tv and show it in my application. I already know how to show the image but I dont know how to read it from the usb. I tried with this code:

File imgFile = new File("sdcard/image/1.jpg");

But it doesn't work for the usb.

Taryn
  • 242,637
  • 56
  • 362
  • 405
user1286765
  • 51
  • 1
  • 3

2 Answers2

1

There is no public API to access the USB devices as a file system. getExternalStorageDirectory() is not the solution.

To address this lack, I suggest to have a look at libmedia, a library designed as a toolbox to help developers to build applications.

As simple as:

VolumeManager volumeManager = new VolumeManager();
List<Volume> volumes = volumeManager.getVolumes();
Volume volume = volumes.get(0);  // simplified
File rootFolder = volume.getRoot();
File imgFile = new File(rootFolder, "image/1.jpg");
andr
  • 15,970
  • 10
  • 45
  • 59
libeasy
  • 379
  • 2
  • 11
  • I tried using this library but it throws "SecurityException: Licensing.allow() has to be called first at fr.maxcom....(SourceFile:50)" when I try to instantiate VolumeManager. I followed the directions: 1) added jar 2) generated api key 3) added api key to manifest. – Mike Ortiz Oct 17 '13 at 00:37
  • My issue was that I was placing the api key in the manifest of my app library (where I was using VolumeManager), rather than in the main app manifest. That said, when I used the example code on the site to enumerate the volumes and files, the logs print out about 20 messages of "Error: null" in VolumeManager and the StringBuilder is empty. This is despite adding a Media Storage Directory as you do in your example. – Mike Ortiz Oct 17 '13 at 18:30
1

I think you need to use:

"/sdCard/...." instead of "sdcard/..."

If you try "sdcard/.." it will read from the files directory of your application, not the SDCard you want. So in your case it will try to open "/data/data/your_project_package_structure/files/sdcard/image/1.jpg"

Also it may help to use the Environment variable to get the directory of your external storage/usb

Uri u = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "blabla.xml"));

Make sure using debug and a file explorer the path is correct.

Robert
  • 10,403
  • 14
  • 67
  • 117
Stephan Celis
  • 2,492
  • 5
  • 23
  • 38
  • 1
    Thanks but this doesn't work because the getExternalStorageDirectory method returns a filesystem that is shared across all aplications and usually it is the sdcard but in devices like the google tv that have multiple external storage, this method returns the primary filesystem that is shared and that filesystem in the google tv is not removable. – user1286765 Mar 23 '12 at 14:48