2011-12-14 2 views
1

기본적으로 가속도계 데이터를 txt 파일로 캡처하려고합니다. 이 코드는 가속 데이터를 문자열에 쓴 다음 자주 바뀔 때마다 업데이트합니다. 데이터 로깅을 시작하는 토글 단추가 포함되어 있으며이를 파일에 기록해야합니다. 이것은 이상한 곳입니다. 코드는 파일이 이미 있는지 확인한 다음 파일 끝에 숫자를 추가하여 이전 데이터를 덮어 쓰지 않도록합니다. 하지만 가비지 또는 손상된 데이터로 가득 찬 파일을 만듭니다. 파일이 이미 존재하면 파일에 좋은 데이터를 쓰지만 파일을 만들어야 할 경우 손상된 파일이 생성되는지 확인했습니다. 코드는 아래와 같습니다. 파일의 손상으로 어떤 일이 일어나고 있는지 알지 못합니다.버퍼링 된 작성기가 새 파일을 만들 때 손상된 데이터를로드합니다.

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{ 

    } 
    } 

내가 손상된 데이터

# EO'MyÎ & ¸ Œ-`ID ïÇDêç³òß¼ ( 살전 û과 로그 파일의 내 매니페스트

"<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>" 

예에서 선언 한 'æÁ9Û ™ íb_CONSOLE · ‡ Zub ™

당신의 생각을 알려주세요.

답변

1

나는 droid X를 가지고 있는데, droid x를 USB 저장 장치로 연결하면 전화 상에 생성되어 저장되는 모든 것이 암호화된다는 것입니다. 그래서 데이터를 볼 때 데이터가 손상된 것 같습니다. PC 모드에서 전화를 연결하면 파일을 적절히 볼 수 있습니다. 나는 이것이 다른 누군가에게 고통과 고통을 덜어주기를 희망합니다. 필자는 거기에 없었던 오류를 수정하기 위해 코드 17 번을 다시 작성했습니다.

관련 문제