0

I have a simple app the I created. I want to write the accelerometer data onto a text file that I create. I've used the following code snippet, but when I check my file, I am not able to get the values writen inside it. I'm not sure what I'm doing wrong. Thank you in advance

P.S I am new to Android

    package com.example.walkpatteranalyser;

import com.example.walkpatteranalyser.R;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity implements SensorEventListener {

    private SensorManager mSensorManager; 
    private Sensor mAccelerometer;
    private FileWriter Writer;

    char co_or_x;
    char co_or_y;
    char co_or_z;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    }

    public void onStartClick(View view) {
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        Toast.makeText(getApplicationContext(), "Started writing to myfile.txt", Toast.LENGTH_SHORT).show();
    }

    public void onStopClick(View view) {
        mSensorManager.unregisterListener(this);
        Toast.makeText(getApplicationContext(), "Stopped", Toast.LENGTH_SHORT).show();
    }
    protected void onResume() {
        super.onResume();

    }

    protected void onPause() {
        super.onPause();

        if( Writer != null) {
           try {
            Writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
}

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];


        try {

            File root = new File(Environment.getExternalStorageDirectory(), "Accleration_Data");
            if (!root.exists()) {
                root.mkdirs();
            }

//            BufferedWriter out = new BufferedWriter(new FileWriter("text.txt"));
//            out.write(Float.toString(event.values[0]) + Float.toString(event.values[1]) + Float.toString(event.values[2]) );
//            out.close();
            File gpxfile = new File(root, "text.txt");
            FileWriter writer = new FileWriter(gpxfile);
    //        writer.append(sBody);
            BufferedWriter out = new BufferedWriter(new FileWriter("text.txt"));
            out.write(Float.toString(event.values[0]) + Float.toString(event.values[1]) + Float.toString(event.values[2]) );
            out.close();
//          writer.flush();
//          writer.close();
            Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
}
Bhargav Panth
  • 301
  • 1
  • 3
  • 10
  • Are you getting the "Saved" toast? Also, why are you trying to write Accelerometer data to a file? One more thing... the onSensorChanged events can come in quite quickly, and you have a lot going on in there.... – Daniel Nugent Mar 01 '15 at 08:24
  • No, I am not getting the "Saved" toast. I am trying to write the accelerometer data to obtain some accelerometer pattern while walking. But I am able to create a folder "Accleration_Data" and also a "text.txt" file inside it. Not able to write the sensor data onto the file. – Bhargav Panth Mar 01 '15 at 08:27
  • Are you going to read this file in you code, process it, and have it dictate how certain functionality works? Or, do you just want to see the data for analysis? If you just want to see the data, the best way is to just log it, and save your logs for analysis. – Daniel Nugent Mar 01 '15 at 08:31
  • I want to just use it for analysis. Okay, thank you Daniel Nuget. – Bhargav Panth Mar 01 '15 at 08:58
  • If you would like to still try getting the file writing working, see answer below. There are also ways to write logs to a file on the sdcard by launching a shell script when your app starts. – Daniel Nugent Mar 01 '15 at 09:11

1 Answers1

0

It looks like the main problem is that you're not using the ExternalStorageDirectory file.txt. You might be writing it to internal storage.

Try this:

        File root = new File(Environment.getExternalStorageDirectory(), "Accleration_Data");
        if (!root.exists()) {
            root.mkdirs();
        }  
        File gpxfile = new File(root, "text.txt");
        FileWriter writer = new FileWriter(gpxfile.getAbsoluteFile());

        BufferedWriter out = new BufferedWriter(writer);
        out.write(String.format("%-10f %-10f %-10f", event.values[0], event.values[1], event.values[2] ));
        out.close();

        Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137