5

我正在做我的项目来记录来自加速度计和陀螺仪传感器的数据并将其保存到 sdcard 中的文件中。但是我有问题,当我开始录制超过 1 秒时,文件中的结果只有 1 个数据,由 1 个 x 轴、1 个 y 轴和 1 个 z 轴组成。理想情况下,我应该在记录文件时在文件中获取更多数据

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    //return inflater.inflate(R.layout.get_data, container, false);

    //get path from sdcard
    sdcard = Environment.getExternalStorageDirectory().getPath();

    rootView = inflater.inflate(R.layout.get_data, container,false);
    Button start = (Button) rootView.findViewById(R.id.buttonstart);
    Button stop = (Button) rootView.findViewById(R.id.buttonstop);

    //eksekusi saat klik button stop
    stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            rekam = false;
            // TODO Auto-generated method stub

            //sensorManager.unregisterListener(mySensorEventListener);
            //super.onStop();


        }
    });

    //eksekusi saat klik button start
    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            rekam = true;
            String nama = sdcard+"/datasensor.txt";
            namafile = new File(nama);



        }
    });
    return rootView;
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    parent = getActivity();
    sensorManager = (SensorManager) parent.getSystemService(Context.SENSOR_SERVICE);
    sensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
    sensorgyroscope = sensorManager.getSensorList(Sensor.TYPE_GYROSCOPE).get(0);

    koordinatX = (TextView)rootView.findViewById(R.id.coordinatx);
    koordinatY = (TextView)rootView.findViewById(R.id.coordinaty);
    koordinatZ = (TextView)rootView.findViewById(R.id.coordinatz);

    koordinatrollX = (TextView)rootView.findViewById(R.id.coordinatrollx);
    koordinatpitchY = (TextView)rootView.findViewById(R.id.coordinatpitchy);
    koordinatyawZ = (TextView)rootView.findViewById(R.id.coordinatyawz);

    sensorManager=(SensorManager) parent.getSystemService(Context.SENSOR_SERVICE);
    //add listener for accelerometer
    sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
    //add listener for gyroscope
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL); 
}

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

}

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER)
    {

        //assign directions accelometer
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        koordinatX.setText("X: "+x);
        koordinatY.setText("Y: "+y);
        koordinatZ.setText("Z: "+z);

        if(rekam==true)
        {
            try
            {
                BufferedWriter out = new BufferedWriter(new FileWriter(namafile));
                out.write(Float.toString(event.values[0]) + Float.toString(event.values[1]) + Float.toString(event.values[2]) );
                out.close();
            }
            catch (IOException e)
            {
                System.out.println("Exception");
            }

        }
    }

    if(event.sensor.getType()==Sensor.TYPE_GYROSCOPE)
    {                   
        float roolX = event.values[0];
        float pitchY = event.values[1];
        float yawZ = event.values[2];


        koordinatrollX.setText("Orientation X (Roll) :" + Float.toString(event.values[0]));
        koordinatpitchY.setText("Orientation Y (Pitch) :" + Float.toString(event.values[1]));
        koordinatyawZ.setText("Orientation Z (Yaw):" + Float.toString(event.values[2]));

        if(rekam==true)
        {
            try
            {
            BufferedWriter out = new BufferedWriter(new FileWriter(namafile));
            out.write(Float.toString(event.values[0]) + Float.toString(event.values[1]) + Float.toString(event.values[2]) );
            out.close();
            }

            catch (IOException e)
            {
                System.out.println("Exception");
            }

        }
}

}
}
4

2 回答 2

2

而不是buf.write(),尝试buf.append()

它会在您使用时附加新的传感器读数而不是过度写入buf.write()

于 2013-04-03T09:28:44.157 回答
2

你必须使用:

BufferedWriter out = new BufferedWriter(new FileWriter(namafile));

里面:

    //eksekusi saat klik button start
start.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        rekam = true;
        String nama = sdcard+"/datasensor.txt";
        namafile = new File(nama);
        //use it here.
        BufferedWriter out = new BufferedWriter(new FileWriter(namafile));
    }
});

还:

out.close();

必须在其他“停止”onClick 内。

//eksekusi saat klik button stop
stop.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        rekam = false;
        out.close();

    }
});

正如 Shehroz 所说,你必须使用,

out.append(Float.toString(event.values[0]) + Float.toString(event.values[1]) + Float.toString(event.values[2]) );

我希望能回答你的问题。

于 2013-10-13T00:12:32.373 回答