基本上,我正在尝试将加速计数据捕获到txt文件中。代码将accel数据写入字符串,然后每次更改时都会更新(通常是这样)。我包括一个切换按钮,它开始记录数据,并应该将其写入文件。这是它变得奇怪的地方。代码检查文件是否已存在,然后在文件末尾添加一个数字,以确保它不会覆盖旧数据。但是,它会创建一个充满垃圾或损坏数据的文件。我验证了如果文件已经存在,那么它会将好的数据写入文件,但如果必须创建文件,则会创建一个损坏的文件。代码如下所示。不知道文件损坏是怎么回事。
package com.accelerometermonitor;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AccelerometerMonitorActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
SensorManager sensorManager;
Sensor accel;
TextView xxaxistext;
TextView yyaxistext;
TextView zzaxistext;
Button toggle;
boolean recordstatus = false;
StringBuilder databuilder;
String data;
TextView testing;
String xstring;
String ystring;
String zstring;
int filecounter=0;
boolean notdone = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
toggle=(Button)findViewById(R.id.togglebutt);
toggle.setOnClickListener(this);
xxaxistext=(TextView)findViewById(R.id.xaxistext);
yyaxistext=(TextView)findViewById(R.id.yaxistext);
zzaxistext=(TextView)findViewById(R.id.zaxistext);
testing=(TextView)findViewById(R.id.testtext);
}
@Override
public void onStart(){
super.onStart();
while (notdone) { //establishes existing logfile number to be used and stored
File logFile = new File("/sdcard/log"+""+ filecounter +".txt");
if (logFile.exists()){
filecounter++;
}
else {
notdone = false;
}
}
}
protected void onResume() {
super.onResume();
sensorManager.registerListener(accelListener, accel,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(accelListener);
}
private SensorEventListener accelListener = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
update(x, y, z);
}
};
public void update (float x, float y, float z){//sets textview to appropriate number
xxaxistext.setText("X Axis:" +""+ x);
yyaxistext.setText("Y Axis:" +""+ y);
zzaxistext.setText("Z Axis:" +""+ z);
//save to string
data="";
xstring=("" + x);
ystring=("" + y);
zstring=("" + z);
data=data.concat(xstring + "," + ystring +"," + zstring +"\n");
// testing.setText("result:" + data);
appendLog(data, filecounter);
}
public void onClick(View target) {
if (recordstatus==false){
toggle.setText("Stop");
recordstatus = true;
appendLog(data, filecounter);
testing.setText("Logging");
}
else{
//Stop recording
recordstatus = false;
toggle.setText("Start");
testing.setText("logging stopped");
filecounter++;
}
}
public void appendLog(String text, int filecounter)
{
if(recordstatus==true){
File logFile = new File("/sdcard/log"+""+ filecounter +".txt");
testing.setText("working");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
Toast toast= Toast.makeText(this,"new file created",0);
toast.show();
BufferedWriter buf1 = new BufferedWriter(new FileWriter(logFile , false));
buf1.write(13);
buf1.newLine();
buf1.flush();
buf1.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
// File logaFile = new File("/sdcard/log"+""+ filecounter +".txt");
//BufferedWriter for performance, true to set append to file flag
//FileWriter logFile=new FileWriter(logaFile , false);
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile , true));
buf.append(text);
buf.newLine();
buf.flush();
buf.close();
testing.setText("append working");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
}
}
我已在我的清单中宣布
"<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>"
包含损坏数据的日志文件示例
#EO'MyÎ&amp;¸Œ-`íÐ ïÇDêç³òß¼( 1号 û'æÁ9Û™íb_CONSOLE·óZub™
让我知道你的想法。
答案 0 :(得分:1)
我有一个droid X,我发现当你将droid x作为usb存储设备连接时,手机上创建和存储的任何东西都是加密的。这就是为什么当我查看数据时,它似乎是腐败的。当我在PC模式下挂机时,它允许我适当地查看文件。我希望这能为别人带来一些痛苦和痛苦。我重写了我的代码17次来修复那个不存在的错误。