【问题标题】:How to save android accelerometer data constantly to csv file?如何将android加速度计数据不断保存到csv文件?
【发布时间】:2017-03-13 19:58:40
【问题描述】:

我是 android 编码的新手。我正在尝试将加速度计从我的 android 保存到 csv 文件。我希望它在应用程序启动时不断记录 x、y 和 z 值(为了每秒钟的参数)。它正在保存到一个 csv 文件,但我只得到 x,y,z 的一个输出。我想我已经使用了 for 循环,但不确定我还需要什么。 `public class Accelerometer_Data 扩展 Activity 实现 SensorEventListener { 私人浮动 lastX, lastY, lastZ;

private SensorManager sensorManager;
private Sensor accelerometer;

private float deltaXMax = 0;
private float deltaYMax = 0;
private float deltaZMax = 0;

private float deltaX = 0;
private float deltaY = 0;
private float deltaZ = 0;

private float vibrateThreshold = 0;

private TextView currentX, currentY, currentZ, maxX, maxY, maxZ;

public Vibrator v;
final int REQUEST_CODE_ASK_PERMISSIONS = 123;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_accelormeter__data);
    initializeViews();
    checkPermissions();
    //  save = (Button) findViewById(R.id.save_btn);

    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
        // success! we have an accelerometer

        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        vibrateThreshold = accelerometer.getMaximumRange() / 2;

    } else {
        // fail! we don't have an accelerometer!
        Toast.makeText(getBaseContext(), "Can't Find Data ", Toast.LENGTH_LONG).show();
    }

    //initialize vibration
    v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
}

public void initializeViews() {
    currentX = (TextView) findViewById(R.id.currentX);
    currentY = (TextView) findViewById(R.id.currentY);
    currentZ = (TextView) findViewById(R.id.currentZ);

    maxX = (TextView) findViewById(R.id.maxX);
    maxY = (TextView) findViewById(R.id.maxY);
    maxZ = (TextView) findViewById(R.id.maxZ);
}

//onResume() register the accelerometer for listening the events
protected void onResume() {
    super.onResume();
    sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

//onPause() unregister the accelerometer for stop listening the events
protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}

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

}

@Override
public void onSensorChanged(SensorEvent event) {
    // clean current values
    displayCleanValues();
    // display the current x,y,z accelerometer values
    displayCurrentValues();
    // display the max x,y,z accelerometer values
    displayMaxValues();

    // get the change of the x,y,z values of the accelerometer
    deltaX = Math.abs(lastX - event.values[0]);
    deltaY = Math.abs(lastY - event.values[1]);
    deltaZ = Math.abs(lastZ - event.values[2]);

    // if the change is below 2, it is just plain noise
    if (deltaX < 2)
        deltaX = 0;
    if (deltaY < 2)
        deltaY = 0;
    if ((deltaX > vibrateThreshold) || (deltaY > vibrateThreshold) || (deltaZ > vibrateThreshold)) {
        v.vibrate(50);
    }

    String entry = currentX.getText().toString() + "," + currentY.getText().toString() + "," + currentZ.getText().toString() + ",";
    try {

        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File(sdCard.getAbsolutePath() + "/sean");
        Boolean dirsMade = dir.mkdir();
        //System.out.println(dirsMade);
        Log.v("Accel", dirsMade.toString());

        File file = new File(dir, "output.csv");
        FileOutputStream f = new FileOutputStream(file);
        f = new FileOutputStream(file);


        try {
            f.write(entry.getBytes());
            f.flush();
            f.close();
            Toast.makeText(getBaseContext(), "Data saved", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
        }


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

public void displayCleanValues() {
    currentX.setText("0.0");
    currentY.setText("0.0");
    currentZ.setText("0.0");
}

// display the current x,y,z accelerometer values
public void displayCurrentValues() {
    currentX.setText(Float.toString(deltaX));
    currentY.setText(Float.toString(deltaY));
    currentZ.setText(Float.toString(deltaZ));
}

// display the max x,y,z accelerometer values
public void displayMaxValues() {
    if (deltaX > deltaXMax) {
        deltaXMax = deltaX;
        maxX.setText(Float.toString(deltaXMax));
    }
    if (deltaY > deltaYMax) {
        deltaYMax = deltaY;
        maxY.setText(Float.toString(deltaYMax));
    }

    if (deltaZ > deltaZMax) {
        deltaZMax = deltaZ;
        maxZ.setText(Float.toString(deltaZMax));
    }
}

private void checkPermissions() {
    int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSIONS);
        return;
    }
    Toast.makeText(getBaseContext(), "Permission is already granted", Toast.LENGTH_LONG).show();
}

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                Toast.makeText(getBaseContext(), "Permission Granted", Toast.LENGTH_LONG).show();
            } else {
                // Permission Denied
                Toast.makeText(Accelerometer_Data.this, "WRITE_EXTERNAL_STORAGE Denied", Toast.LENGTH_SHORT).show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

}`

【问题讨论】:

    标签: java android csv


    【解决方案1】:

    我认为您总是覆盖自己的文件。

    首先这里的代码加倍,替换这个:

    FileOutputStream f = new FileOutputStream(file);
    f = new FileOutputStream(file);
    

    用这个:

    FileOutputStream f = new FileOutputStream(file, true);
    

    在这里你可以看到更多关于FileOutputStreamhttps://developer.android.com/reference/java/io/FileWriter.html

    第二个参数,布尔值,启用“附加”模式,这样您就不会在每次打开文件时都覆盖它。

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2016-08-08
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2022-08-12
      • 2020-10-11
      • 1970-01-01
      相关资源
      最近更新 更多