OK, I'm just starting out with my first non-trivial app using Eclipse and I'm having some issues. I'm trying to write accelerometer data to a file on the sd-card.
In my Activity class I have a couple of things for file access:
BufferedWriter out;
FileWriter gpxwriter;
Inside onCreate I set them up and do a test write to the file:
This code works, and I can open the file and see the text. great. If I exit at this point all is fine and dandy.
But.... my activity class implements SensorEventListener, so I've implemented the following which gets called every time the sensor data changes. At that point I can see the accel data get updated in real time on the screen using settext calls. works great.....
I let the app go for a few minutes and hit the exit button which executes:
I can see all the events get updated in my LogCat window using DDMS.
The problem is that as soon as I hit my 'exit' button, I get a flurry on java.IO.BufferedWriter java.io.IOExceptions and these continue to scroll by even after the app has been exited and off the screen for minutes:
I obviously doing something wrong, any ideas?
Am I not closing the file correctly?
Why do I see IO errors even after the app is long gone and no longer running?
can I save data in this manner (there are going to of file accesses)?
In my Activity class I have a couple of things for file access:
BufferedWriter out;
FileWriter gpxwriter;
Inside onCreate I set them up and do a test write to the file:
Code:
Log.d("AccelerometerText", "onCreate ... Done");
File gpxfile = new File(root, "RawAccelData2.txt");
FileWriter gpxwriter = new FileWriter(gpxfile);
out = new BufferedWriter(gpxwriter);
out.write("Time (ms); X Value (g);Y Value(g);Z Value (g)");
But.... my activity class implements SensorEventListener, so I've implemented the following which gets called every time the sensor data changes. At that point I can see the accel data get updated in real time on the screen using settext calls. works great.....
Code:
public void onSensorChanged(SensorEvent sensorEvent) {
synchronized (this) {
long mytime = 0; //System.currentTimeMillis();
textAccelerationX.setText("X Acceleration time:" + mytime + " " + Float.toString(sensorEvent.values[0]));
textAccelerationY.setText("Y Acceleration time:" + mytime + " " + Float.toString(sensorEvent.values[1]));
textAccelerationZ.setText("Z Acceleration time:" + mytime + " " + Float.toString(sensorEvent.values[2]));
BigBuf[runningCount] = Float.toString(sensorEvent.values[0]);
if(runningCount==99) {
runningCount=0;
EnterCount++;
Log.d("onSensorChanged", "EnterCount " + EnterCount);
Log.d("onSensorChanged", "out " + out.toString());
try {
[B] out.write("Hello");
out.newLine();[/B]
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
runningCount++;
}
}
Code:
case R.id.ButtonExit:
Log.d("AccelerometerText", "onClick ... buttonexit");
RunningZaccel+=0.8;
try {
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.finish();
break;
}
I can see all the events get updated in my LogCat window using DDMS.
The problem is that as soon as I hit my 'exit' button, I get a flurry on java.IO.BufferedWriter java.io.IOExceptions and these continue to scroll by even after the app has been exited and off the screen for minutes:
Code:
D/onSensorChanged(15160): EnterCount 1541
D/onSensorChanged(15160): out java.io.BufferedWriter@44763778
W/System.err(15160): java.io.IOException: Writer is closed.
W/System.err(15160): at java.io.BufferedWriter.write(BufferedWriter.java:308)
W/System.err(15160): at java.io.Writer.write(Writer.java:152)
W/System.err(15160): at com.example.AccelerometerTest.AccelerometerTest.onSensorChanged(AccelerometerTest.java:202)
W/System.err(15160): at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:449)
W/System.err(15160): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(15160): at android.os.Looper.loop(Looper.java:123)
W/System.err(15160): at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err(15160): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(15160): at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err(15160): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
W/System.err(15160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
W/System.err(15160): at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm(15160): GC_FOR_MALLOC freed 7981 objects / 523992 bytes in 65ms
D/onSensorChanged(15160): EnterCount 1542
I obviously doing something wrong, any ideas?
Am I not closing the file correctly?
Why do I see IO errors even after the app is long gone and no longer running?
can I save data in this manner (there are going to of file accesses)?