如何将数据添加到android

时间:2016-04-25 03:24:58

标签: android file-io accelerometer

我正在开发一个应用程序来读取来自加速度计的数据,我想将它们保存在.txt文件中,以便我以后可以处理它们。

到目前为止,我已经能够获得读数并且只保存一个读数,因为据我所知,我总是创建一个覆盖以前存在的文件的新文件。但我想要的是从我按下开始按钮的那一刻起进行所有测量,直到我按下停止按钮。

以下是我使用的代码:

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;
    }
}

}

MyFile Class,工作完成的地方如下:

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;
}

}

我遇到的另一个问题是我无法将.txt文件存储在SD卡中,并将其保存在内部存储器中。

感谢您的时间和帮助

1 个答案:

答案 0 :(得分:2)

试试这段代码。将FileWriter的第二个参数设置为 true 。因此,您可以附加现有文件

 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();