1

I need to be able to store accelerometer data in a text file on Google Glass, plug the glass into a computer, and then be able to retrieve that written file.

I am not getting any errors/exceptions--the file just never appears.

Here's what I have so far (that doesn't work):

public void onCreate(Bundle savedInstanceState) {
    ...
    try{
        accFile = openFileOutput("acc_data.csv",  MODE_APPEND | MODE_WORLD_READABLE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
}

Gathering the sensor data is definately not a problem since I can seex, y, and z if I set a breakpoint.

public void onSensorChanged(SensorEvent event) {
    Sensor sensor = event.sensor;

    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
    String outString = "\""+currentDateTimeString+"\","+x+","+y+","+z+"\n";

    if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        try {
            OutputStreamWriter osw = new OutputStreamWriter(accFile);
            osw.write(outString);
            osw.flush();
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public void onDestroy() {
    ...
    if(accFile != null)
    {
        try {
            accFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

And just in case, the android manifest has android.permission.WRITE_EXTERNAL_STORAGE

Shane Wilhelm
  • 95
  • 1
  • 6

1 Answers1

1

openFileOutput will create/write to a file that is internal to your application (the parameter MODE_WORLD_READABLE has been deprecated). Ideally, this means you should not be able to access this file from a computer.

Use an external storage public directory instead:

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
file = new File(path, filename);

To perform writes, use a FileOutputStream:

os = new FileOutputStream(file, true);
os.write(string.getBytes());

To perform reads from within your app, use a FileInputStream:

FileInputStream fis = new FileInputStream(file);
int c;
String temp = "";
while ( (c = fis.read()) != -1) {
     temp = temp + " | " + Character.toString((char) c);
}
Log.v(TAG, temp);

Lastly, to access your file through adb shell, simply access the directory returned by path.toString().

Koh
  • 1,570
  • 1
  • 8
  • 6