我创建了一个简单的应用程序。我想将加速度计数据写入我创建的文本文件中。我使用了以下代码段,但是当我检查我的文件时,我无法获取其中的值。我不确定我做错了什么。 提前谢谢
P.S我是Android新手
package com.example.walkpatteranalyser;
import com.example.walkpatteranalyser.R;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private FileWriter Writer;
char co_or_x;
char co_or_y;
char co_or_z;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
public void onStartClick(View view) {
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(getApplicationContext(), "Started writing to myfile.txt", Toast.LENGTH_SHORT).show();
}
public void onStopClick(View view) {
mSensorManager.unregisterListener(this);
Toast.makeText(getApplicationContext(), "Stopped", Toast.LENGTH_SHORT).show();
}
protected void onResume() {
super.onResume();
}
protected void onPause() {
super.onPause();
if( Writer != null) {
try {
Writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
try {
File root = new File(Environment.getExternalStorageDirectory(), "Accleration_Data");
if (!root.exists()) {
root.mkdirs();
}
// BufferedWriter out = new BufferedWriter(new FileWriter("text.txt"));
// out.write(Float.toString(event.values[0]) + Float.toString(event.values[1]) + Float.toString(event.values[2]) );
// out.close();
File gpxfile = new File(root, "text.txt");
FileWriter writer = new FileWriter(gpxfile);
// writer.append(sBody);
BufferedWriter out = new BufferedWriter(new FileWriter("text.txt"));
out.write(Float.toString(event.values[0]) + Float.toString(event.values[1]) + Float.toString(event.values[2]) );
out.close();
// writer.flush();
// writer.close();
Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
看起来主要的问题是你没有使用ExternalStorageDirectory file.txt。您可能正在将其写入内部存储。
试试这个:
File root = new File(Environment.getExternalStorageDirectory(), "Accleration_Data");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, "text.txt");
FileWriter writer = new FileWriter(gpxfile.getAbsoluteFile());
BufferedWriter out = new BufferedWriter(writer);
out.write(String.format("%-10f %-10f %-10f", event.values[0], event.values[1], event.values[2] ));
out.close();
Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();