2

I am developing an application that reads data coming from the accelerometer and I want to save them in .txt file, so that I can later process them.

So far I have been able to get the readings and also to save just one reading, as from what I understand I always create a new file that overwrites the previously existing file. But I want is to have all the measurements from the moment I push the start button, until I push the stop button.

Here is the code I'm using:

public class MainActivity extends AppCompatActivity implements SensorEventListener, View.OnClickListener {

private SensorManager mSensorManager;

private Sensor mAccelerometer;
private Button bStart, bStop;
float[] acceleration = new float[3];
private String mString;

MyFile file = new MyFile(this);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bStart = (Button)findViewById(R.id.start);
    bStop = (Button)findViewById(R.id.stop);

    mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    bStart.setOnClickListener(this);
    bStop.setOnClickListener(this);
    bStart.setEnabled(true);
    bStop.setEnabled(false);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}

public final void onSensorChanged(SensorEvent event){
    if(event.sensor.getType() ==  Sensor.TYPE_ACCELEROMETER){
        acceleration[0] = event.values[0];
        acceleration[1] = event.values[1];
        acceleration[2] = event.values[2];
        TextView mTextview1 = (TextView) findViewById(R.id.textView1);
        TextView mTextview2 = (TextView) findViewById(R.id.textView2);
        TextView mTextview3 = (TextView) findViewById(R.id.textView3);
        mTextview1.setText("X:"+String.valueOf( acceleration[0]));
        mTextview2.setText("Y:"+String.valueOf( acceleration[1]));
        mTextview3.setText("Z:"+String.valueOf( acceleration[2]));

        file.writeToSD(acceleration[0] + "," +acceleration[1] + "," +acceleration[2] + "\n");
    }
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.start:
            bStart.setEnabled(false);
            bStop.setEnabled(true);
            mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
            break;
        case R.id.stop:
            bStart.setEnabled(true);
            bStop.setEnabled(false);
            mSensorManager.unregisterListener(this);
            break;
    }
}

}

And MyFile Class, where the job gets done is the following:

public class MyFile {

String TAG = "MyFile";
Context context;

public MyFile(Context context) {
    this.context = context;
}

public Boolean writeToSD(String text) {
    Boolean write_successful = false;
    File root = null;
    try {
        // check for SDcard
        root = Environment.getExternalStorageDirectory();
        Log.i(TAG, "path.." + root.getAbsolutePath());

        //check sdcard permission
        if (root.canWrite()) {
            File fileDir = new File(root.getAbsolutePath());
            fileDir.mkdirs();

            File file = new File(fileDir, "samplefile.txt");
            FileWriter filewriter = new FileWriter(file);
            BufferedWriter out = new BufferedWriter(filewriter);
            out.append(text);
            out.flush();
            out.close();
            write_successful = true;
        }
    } catch (IOException e) {
       e.printStackTrace();
        write_successful = false;
    }
    return write_successful;
}

}

Another question that I have is why I can not store the .txt file in the sdcard and it is saved in the internal memory.

Thank you for your time and your help

user2149122
  • 171
  • 1
  • 3
  • 14
  • 3
    Pass 2nd parameter true to append in file. Like this FileWriter fw = new FileWriter("outfilename", true); – Wasim K. Memon Apr 25 '16 at 03:32
  • Thank you very much. It worked. By the way, can I ask you why using this code the .txt file is saved in the internal memory of the device and not in the sdcard? I have used the permissions – user2149122 Apr 25 '16 at 09:39
  • you are testing in emulator or device ? you can check what path it prints in logger. you have already printed that path. so check it. – Wasim K. Memon Apr 25 '16 at 09:48
  • It gives back a path in the internal memory. I can not figure out why this is happening. And I'm testing on both. In the actual device I find the file in the internal memory. In the emulator I do not have access to the internal memory – user2149122 Apr 25 '16 at 09:53
  • for that you can check this - http://stackoverflow.com/a/6049446/2128166 – Wasim K. Memon Apr 25 '16 at 10:02

1 Answers1

5

Try this code. Set second argument of FileWriter to true. Thus you can append your existing file

 File file = new File("Hello.txt");    
 file.createNewFile();    
 FileWriter writer = new FileWriter(file,true); 
 writer.write("Writes the content to the file"); 
 writer.flush();
 writer.close();
Vikash Kumar Verma
  • 1,068
  • 2
  • 14
  • 30