cthulhu
cthulhu

Reputation: 169

how to create an save sensor data in a csv file in internal memory

I'm trying to get the accelerometer data in a file on internal storage , i tried many ways they but i get errors like java.io.FileNotFoundException: Value.csv: open failed: EROFS (Read-only file system) or ESIDIR.

@RequiresApi(api = Build.VERSION_CODES.N)
public class MainActivity extends Activity implements SensorEventListener,View.OnClickListener {

private TextView xText, yText, zText;
private Sensor sensor;
private SensorManager sManager;
private Button startB, stopB;
private boolean mInitialized;


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

    //Create sensor manager
    sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    // Accelerometer sensor
    sensor = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    //Register sensor Listener
    sManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    // assign Textview
    xText = (TextView) findViewById(R.id.xText);

    Button startButton = (Button) findViewById(R.id.startB);
    Button stopButton = (Button) findViewById(R.id.stopB);
    startButton.setOnClickListener(this);
    stopButton.setOnClickListener(this);
    mInitialized = false;
}

public void onResume() {
    super.onResume();
    sManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}

protected void onPause() {
    super.onPause();
    sManager.unregisterListener(this);
}


public void onAccuracyChanged(Sensor sensor, int acc) {
}


public void onSensorChanged(SensorEvent event) {
    xText.setText(" X  :" + Float.toString(event.values[0]) + "\n" +
            " Y  :" + Float.toString(event.values[1]) + "\n" +
            " Z :" + Float.toString(event.values[2]));

    try {
        writeToCsv(Float.toString(event.values[0]), Float.toString(event.values[1]), Float.toString(event.values[2]));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.startB:
            sManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
            break;
        case R.id.stopB:
            sManager.unregisterListener(this);
            break;
    }
}

public void writeToCsv(String x, String y, String z) throws IOException {
    Calendar c = Calendar.getInstance();

    File path = Environment.getDataDirectory();
    boolean success = true;
    if (!path.exists()) {
        success = path.mkdir();
    }
    if (success) {
        String csv = "Value.csv";
        FileWriter file_writer = new FileWriter(csv, true);
        String s = c.get(Calendar.HOUR) + "," + c.get(Calendar.MINUTE) + "," + c.get(Calendar.SECOND) + "," + c.get(Calendar.MILLISECOND) + "," + x + "," + y + "," + z + "\n";
        file_writer.append(s);
        file_writer.close();

    }
}

}

Upvotes: 0

Views: 647

Answers (1)

greenapps
greenapps

Reputation: 11224

File path = Environment.getDataDirectory();

Will deliver path /data. Which is not readable and not writable as you have seen. Change to for instance

File path = getFilesDir();

Upvotes: 1

Related Questions