【问题标题】:How to save the streaming sensor data internally on Android?如何在 Android 内部保存流式传感器数据?
【发布时间】:2020-03-18 12:05:43
【问题描述】:

干杯。我对 Android 编程比较陌生。我一直在尝试实现一个读取传感器数据(加速度计+陀螺仪)的应用程序,然后通过单击按钮在内部保存它。我能够成功地从传感器获取读数。但是,只有一串数据以文件格式保存,其中仅显示当前数据和以前的数据被覆盖。 Please refer here for the output。因此,我需要一些帮助来解决这个问题。

在 MainActivity.java 中,我注册了传感器监听器:

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

// Obtain the sensor data from the phone
boolean IsDataRequested = false;
@Override
public void onSensorChanged(SensorEvent event) {
    Sensor sensor = event.sensor;
    if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        if (IsDataRequested == true){

            Log.d("Accelerometer", "Acc_X:" + event.values[0] + "Acc_Y:" + event.values[1] + "Acc_Z:" + event.values[2]);

            AccXText.setText("AccX:" + event.values[0]);
            AccYText.setText("AccY:" + event.values[1]);
            AccZText.setText("AccZ:" + event.values[2]);

            save(event);
        }
    }

    if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {

        if (IsDataRequested == true) {

            Log.d("Gyroscope", "Gyro_X:" + event.values[0] + "Gyro_Y:" + event.values[1] + "Gyro_Z:" + event.values[2]);
            GyroXText.setText("GyroX:" + event.values[0]);
            GyroYText.setText("GyroY:" + event.values[1]);
            GyroZText.setText("GyroZ:" + event.values[2]);

            save(event);
        }
    }

}

然后在同一个文件中,我创建了保存方法:

public void save(SensorEvent v) {
    float Acc_X = v.values[0];
    float Acc_Y = v.values[1];
    float Acc_Z = v.values[2];

    float Gyro_X = v.values[0];
    float Gyro_Y = v.values[1];
    float Gyro_Z = v.values[2];

    String accString = "Acc=" + "X:" + Acc_X + "Y:" + Acc_Y + "Z:" + Acc_Z;
    String gyroString = "Gyro=" + "X:" + Gyro_X + "Y:" + Gyro_Y + "Z:" + Gyro_Z;

    String FILENAME = "user";

    FileOutputStream fos = null;
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        fos.write(accString.getBytes());
        fos.write(gyroString.getBytes());
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

按钮点击开始和停止发送数据:

private OnClickListener myOnSensorsRequestClickHandler = new OnClickListener() {
    @Override
            public void onClick(View sensors) {

            IsDataRequested = !IsDataRequested;
            Log.d("Sensors", "Sensors Button Pressed");

    }
};

我的目标是:

  1. 连续保存传感器数据流,直到单击同一按钮停止读取数据,然后将其保存在一个文件中。

  2. 下次我开始和停止点击按钮时,它会添加另一个保存的文件,这样我就可以在一个文件夹中保存多个文件。

非常感谢任何帮助或建议:)

【问题讨论】:

    标签: java android save android-sensors


    【解决方案1】:

    我希望以下建议对您的以下要求有所帮助

        private OnClickListener myOnSensorsRequestClickHandler = new OnClickListener() {
        @Override
                public void onClick(View sensors) {
    
                IsDataRequested = !IsDataRequested;
                Log.d("Sensors", "Sensors Button Pressed");
    
                if(IsDataRequested)
                {
                //Create file name when start service button was clicked
            FILENAME = "user"+new Date().getTime() + ".txt"; //Here FILENAME is a path of file in global variable
            }
    
        }};
    
    
    
        boolean IsDataRequested = false;
        @Override
        public void onSensorChanged(SensorEvent event) {
            Sensor sensor = event.sensor;
            if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
    
                if (IsDataRequested == true){
    
    
    
                    Log.d("Accelerometer", "Acc_X:" + event.values[0] + "Acc_Y:" + event.values[1] + "Acc_Z:" + event.values[2]);
    
                    AccXText.setText("AccX:" + event.values[0]);
                    AccYText.setText("AccY:" + event.values[1]);
                    AccZText.setText("AccZ:" + event.values[2]);
    
                    save(FILENAME, event);
                }
                else
                {
    
                }
            }
    
            if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
    
                if (IsDataRequested == true) {
    
                    Log.d("Gyroscope", "Gyro_X:" + event.values[0] + "Gyro_Y:" + event.values[1] + "Gyro_Z:" + event.values[2]);
                    GyroXText.setText("GyroX:" + event.values[0]);
                    GyroYText.setText("GyroY:" + event.values[1]);
                    GyroZText.setText("GyroZ:" + event.values[2]);
    
                    save(FILENAME, event);
                }
            }
    
        }
        public void save(String FILENAME, SensorEvent v) {
            float Acc_X = v.values[0];
            float Acc_Y = v.values[1];
            float Acc_Z = v.values[2];
    
            float Gyro_X = v.values[0];
            float Gyro_Y = v.values[1];
            float Gyro_Z = v.values[2];
    
    
    //Here used StringBuffer instead of string for append string data
    
            StringBuffer accQyroData = "========";
            accQyroData.append("Acc=" + "X:" + Acc_X + "Y:" + Acc_Y + "Z:" + Acc_Z"+"\n"+"Gyro=" + "X:" + Gyro_X + "Y:" + Gyro_Y + "Z:" + Gyro_Z);
            accQyroData.append("========");
    
            //Use Stream for java append to file when you are dealing with raw data, binary
            appendUsingFileOutputStream(FILENAME, accQyroData);
        }
    
            /**
             * Use Stream for java append to file when you are dealing with raw data, binary
             * data
             * 
             * @param data
             */
            private static void appendUsingFileOutputStream(String fileName, String data) {
                OutputStream os = null;
                try {
                    // below true flag tells OutputStream to append
                    os = new FileOutputStream(new File(fileName), true);
                    os.write(data.getBytes(), 0, data.length());
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
    • 连续保存传感器数据流直到相同 单击按钮停止读取数据,然后将其保存在 一个文件。
    • 下次我开始和停止点击按钮时,它会添加 另一个保存的文件,这样我就可以在一个文件夹中保存多个文件。

    【讨论】:

    • 感谢您的回复!我确实按照您的建议进行了更改,但几乎没有出现错误。似乎它无法解析保存中的“FILENAME”符号和 appendUsingFileOutputStream 中的“OutputStream”符号。我对这些有点困惑。
    • 这里我提到了“FILENAME”是一个全局字符串变量。对于 OutputStream 是一个用于文件读取的类。请参考以下链接developer.android.com/reference/java/io/OutputStream
    猜你喜欢
    • 1970-01-01
    • 2015-09-03
    • 2013-02-03
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多