我想编写csv文件,包括android应用程序中的source注意到的信息。我在android studio中写了源代码,如下图所示。我不知道如何编辑该源以执行我想要的操作。如果我实现该应用程序,tv_ax中的消息是 错误:\ SD卡\模拟\ 0 \ AndroSensor \ output.csv 打开失败:EROFS(只读文件系统) 请帮我。感谢您阅读我的文章。
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.*;
public class MainActivity extends Activity implements SensorEventListener {
TextView tv_ax;
Button bt_load=null;//Load button
Button bt_load_end=null;// Load quit button
SensorManager sm=null;
Sensor accSensor=null;
String csv = "\\storage\\emulated\\0\\AndroSensor\\output.csv";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
accSensor= sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
tv_ax=(TextView)findViewById(R.id.Tv_AX);
bt_load=(Button)findViewById(R.id.bt_load);
bt_load_end=(Button)findViewById(R.id.bt_load_end);
bt_load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sensor_listener();// To register sensor
}
});
}
private void sensor_listener() {
sm.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
try {
CSVWriter writer;
writer = new CSVWriter(new FileWriter(csv));
List<String[]> data=new ArrayList<String[]>();
data.add(new String[] {String.valueOf(" X축:"+(int)event.values[0])});
data.add(new String[]{ String.valueOf(" Y축:" + (int) event.values[1])});
data.add(new String[]{ String.valueOf(" Z축:" + (int) event.values[2])});
writer.writeAll((List<String[]>) data);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
tv_ax.setText("ERROR:"+e.getMessage());
}
break;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
&#13;