私は,Android Stadioで,加速度と地磁気を同時に測定し,CSVファイルに変換し,保存するアプリを作成しています.
現段階では,加速度と地磁気を同時に画面上に表示できている状態ですが,CSVファイルに変換し保存ができていません.データは,SDカードに保存するように書いています.
どうか,ご指導をよろしくお願いします.JAVA言語です.
MainActivity
1 2package com.example.myapplication; 3 4 5import java.io.BufferedWriter; 6import java.io.FileNotFoundException; 7import java.io.FileOutputStream; 8import java.io.IOException; 9import java.io.OutputStreamWriter; 10import java.io.UnsupportedEncodingException; 11import java.util.ArrayList; 12import java.util.Calendar; 13import java.util.List; 14 15import android.app.Activity; 16import android.hardware.Sensor; 17import android.hardware.SensorEvent; 18import android.hardware.SensorEventListener; 19import android.hardware.SensorManager; 20import android.os.Bundle; 21import android.widget.TextView; 22 23public class MainActivity extends Activity implements SensorEventListener, Runnable{ 24 25 26 private boolean mRegisteredSensor; 27 private SensorManager mSensorManager; 28 29 // 加速度センサーの値 30 private float[] accelerometerValues = new float[3]; 31 private float[] magneticValues = new float[3]; 32 33 34 35 36 37 @Override 38 public void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.activity_main); 41 42 43 44 mRegisteredSensor = false; 45 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 46 ArrayList<List<Sensor>> sensors = new ArrayList<List<Sensor>>(); 47 sensors.add( mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER)); 48 sensors.add( mSensorManager.getSensorList(Sensor.TYPE_GYROSCOPE)); 49 50 for(List<Sensor> sensor : sensors){ 51 if(sensor.size()>0){ 52 mRegisteredSensor = mSensorManager.registerListener(this, 53 sensor.get(0), 54 SensorManager.SENSOR_DELAY_NORMAL); 55 } 56 } 57 } 58 59 @Override 60 protected void onResume() { 61 super.onResume(); 62 63 64 { 65 ArrayList<List<Sensor>> sensors = new ArrayList<List<Sensor>>(); 66 sensors.add( mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER)); 67 sensors.add( mSensorManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD)); 68 69 for(List<Sensor> sensor : sensors){ 70 if(sensor.size()>0){ 71 mRegisteredSensor = mSensorManager.registerListener(this, 72 sensor.get(0), 73 SensorManager.SENSOR_DELAY_NORMAL); 74 } 75 } 76 } 77 78 79 } 80 81 @Override 82 protected void onPause() { 83 if (mRegisteredSensor) { 84 mSensorManager.unregisterListener(this); 85 mRegisteredSensor = false; 86 } 87 88 super.onPause(); 89 } 90 91 @Override 92 public void onAccuracyChanged(Sensor sensor, int accuracy) { 93 } 94 95 96 @Override 97 public void onSensorChanged(SensorEvent event ) { 98 { 99 Calendar time = Calendar.getInstance(); 100 101 int year = time.get(time.YEAR); 102 103 int month = time.get(time.MONTH)+1; 104 105 int day = time.get(time.DAY_OF_MONTH); 106 107 int hour = time.get(time.HOUR_OF_DAY); 108 109 int minute = time.get(time.MINUTE); 110 111 int second = time.get(time.SECOND); 112 113 int ms = time.get(time.MILLISECOND); 114 115 116 String nowtime = String.valueOf(year)+"/"+String.valueOf(month)+"/"+String.valueOf(day)+","+ String.valueOf(hour)+":"+String.valueOf(minute)+":"+ String.valueOf(second)+":" +String.valueOf(ms); 117 118 119 120 121 122 if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 123 accelerometerValues = event.values.clone(); 124 } 125 else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){ 126 magneticValues = event.values.clone(); 127 } 128 String FILE = "/sdcard/test.csv"; 129 try { 130 131 132 133 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( 134 135 new FileOutputStream(FILE, true), "UTF-8")); 136 137 String write_int =nowtime + "," + 138 139 String.valueOf(accelerometerValues[0])+"," + 140 141 String.valueOf(accelerometerValues[1])+"," + 142 143 String.valueOf(accelerometerValues[2])+","+ 144 145 String.valueOf(magneticValues[0])+","+ 146 147 String.valueOf(magneticValues[1])+"," + 148 149 String.valueOf(magneticValues[2])+"\n" ; 150 151 152 153 154 bw.write(write_int); 155 156 bw.close(); 157 158 } catch (UnsupportedEncodingException k) { 159 160 k.printStackTrace(); 161 162 } catch (FileNotFoundException k) { 163 164 k.printStackTrace(); 165 166 } catch (IOException k) { 167 168 k.printStackTrace(); 169 170 } 171 172 TextView x = (TextView)findViewById(R.id.x); 173 174 x.setText("x" + String.valueOf(accelerometerValues[0])); 175 176 TextView y = (TextView)findViewById(R.id.y); 177 y.setText("y" + String.valueOf(accelerometerValues[1])); 178 179 TextView z = (TextView)findViewById(R.id.z); 180 z.setText("z" + String.valueOf(accelerometerValues[2])); 181 182 TextView a = (TextView)findViewById(R.id.a); 183 a.setText("a" + String.valueOf(magneticValues[0])); 184 185 TextView b = (TextView)findViewById(R.id.b); 186 b.setText("b" + String.valueOf(magneticValues[1])); 187 188 TextView c = (TextView)findViewById(R.id.c); 189 c.setText("c" + String.valueOf(magneticValues[2])); 190 191 192 193 194 195// text.setText( 196// "x " + accelerometerValues[0] + ", \n" + 197// "y " + accelerometerValues[1] + ", \n" + 198// "z " + accelerometerValues[2] + ", \n" + 199// "地磁気"+"\n"+ 200// "mgx " + magneticValues[0] + ", \n" + 201// "mgy " + magneticValues[1] + ", \n" + 202// "mgz " + magneticValues[2] 203// 204// 205// ); 206 207 } 208 209 } 210 211 @Override 212 public void run() { 213 // TODO Auto-generated method stub 214 215 } 216 217 218 @Override 219 220 protected void onStop() 221 222 { 223 224 super.onStop(); 225 226 mSensorManager.unregisterListener(this); 227 228 } 229 230} 231 232
JAVA
1<?xml version="1.0" encoding="utf-8"?> 2 3<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 5 android:orientation="vertical" 6 7 android:layout_width="fill_parent" 8 9 android:layout_height="fill_parent" 10 11 > 12 13 <TextView 14 15 android:layout_width="fill_parent" 16 17 android:layout_height="wrap_content" 18 19 android:text="加速度" 20 21 /> 22 23 <TextView 24 25 android:id="@+id/x" 26 27 android:layout_width="fill_parent" 28 29 android:layout_height="wrap_content" 30 31 /> 32 33 <TextView 34 35 android:id="@+id/y" 36 37 android:layout_width="fill_parent" 38 39 android:layout_height="wrap_content" 40 41 /> 42 <TextView 43 44 android:id="@+id/z" 45 46 android:layout_width="fill_parent" 47 48 android:layout_height="wrap_content" 49 50 /> 51 52 <TextView 53 54 android:layout_width="fill_parent" 55 56 android:layout_height="wrap_content" 57 58 android:text="地磁気" 59 60 /> 61 62 <TextView 63 64 android:id="@+id/a" 65 66 android:layout_width="fill_parent" 67 68 android:layout_height="wrap_content" 69 70 /> 71 <TextView 72 73 android:id="@+id/b" 74 75 android:layout_width="fill_parent" 76 77 android:layout_height="wrap_content" 78 79 /> 80 <TextView 81 82 android:id="@+id/c" 83 84 android:layout_width="fill_parent" 85 86 android:layout_height="wrap_content" 87 88 /> 89 90 91</LinearLayout> 92 93 94
回答1件
あなたの回答
tips
プレビュー